
The get(TemporalField field) method from LocalDateTime can be used to get the value of the specified field from the current object as an integer. Let's take a look at some of the examples of how to use it,
LocalDateTime localDateTime = LocalDateTime.now(ZoneId.of("UTC"));
System.out.println("Current Date Time (UTC/GMT): " + localDateTime);
Current Date Time (UTC/GMT): 2022-05-14T09:27:38.319137
We have a LocalDateTime object in Zone UTC and now we are going to make use of the get(TemporalField field) methods over it to get various fields from it,
Get the Year
int year = localDateTime.get(ChronoField.YEAR); System.out.println("Year : " + year); //Output: 2022
Get the Month of the Year
int monthOfYear = localDateTime.get(ChronoField.MONTH_OF_YEAR); System.out.println("Day of the Month: " + monthOfYear); //Output: 5
Get the Day of the Month
int monthOfYear = localDateTime.get(ChronoField.MONTH_OF_YEAR); System.out.println("Day of the Month: " + monthOfYear); //Output: 14
Get the Hour of the Day
int hourOfDay = localDateTime.get(ChronoField.HOUR_OF_DAY); System.out.println("Hour of the Day : " + hourOfDay); //Output: 9
Get the Minutes of the Hour
int minuteOfHour = localDateTime.get(ChronoField.MINUTE_OF_HOUR); System.out.println("Minute of the Hour : " + minuteOfHour); //Output: 27
Get the Seconds of the Minute
int secondOfMinute = localDateTime.get(ChronoField.SECOND_OF_MINUTE); System.out.println("Second of the Minute : " + secondOfMinute); //Output: 38
Get the Seconds of the Minute
int millSecOfSecond = localDateTime.get(ChronoField.MILLI_OF_SECOND); System.out.println("Mill-second of the Second : " + millSecOfSecond); //Output: 319137
Now let's look at some more fields that are not displayed as a part of the default LocalDateTime toString output,
Get the Seconds of the Day
int secondOfDay = localDateTime.get(ChronoField.SECOND_OF_DAY); System.out.println("Seconds of Day : " + secondOfDay); //Output: 96024
Get the Day of the Year
int dayOfYear = localDateTime.get(ChronoField.DAY_OF_YEAR); System.out.println("Day of the Year : " + dayOfYear); //Output: 134
Get the Seconds of the Minute
int secondOfMinute = localDateTime.get(ChronoField.SECOND_OF_MINUTE); System.out.println("Second of the Minute : " + secondOfMinute); //Output: 38
Get the AM/PM of the Day
//AM=0 PM=1 int AmorPM = localDateTime.get(ChronoField.AMPM_OF_DAY); System.out.println(AmorPM); //Output: 0
More Posts related to Java,
- How to install Java 11 on Mac
- Get Client IP address from HTTP Response in Java
- SharePoint Open in the client application document opens in browser
- How to verify if java is installed on the computer and get version detail
- Java - Check if array contains the value
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- How to run Java Unit Test cases with Apache Maven?
- What Java version is used for Minecraft 1.18
- List of jar files for Jax-ws (SOAP) based Java Web Services
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- Convert Instant timestamp into LocalDateTime Java Code Example
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Create simple struts2 project using maven commands
- Java - PatternSyntaxException
- List of Online Java compiler with console
- Minecraft Java Edition
- How to declare and initialize Array in Java Programming
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- Java XML-RPC 3.1.x based web service example
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server
- Java 8 foreach loop code examples
- Java -Day of the week using Java 8 DayOfWeek Enum
- Java 8 - Convert List to Map Examples
- Java: TimeZone List with GMT/UTC Offset
- list of jars required for hibernate 4.x.x
More Posts:
- The authenticated user is not authorized to access this resource - Stream/SharePoint/Teams/OneNote - Microsoft
- PowerShell - How to use Try Catch Finally blocks for error exception handling (Windows/SharePoint) - SharePoint
- Redirect page using jQuery - jQuery
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server - Java
- Android : Unable to load VM from snapshot : Mac OS X Error - Android
- Android : Accidental Octal Lint Warning - Android
- How to reset eclipse layout - Android
- Examples: Convert String to int in JavaScript - JavaScript