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,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- How to find current logged-in user in Linux Terminal - Linux
- bash: ls command to see list files in current directory all subdirectories - Bash
- Run only a Single Unit Test using Gradle - Gradle
- [Fix] Steam Friends Network Unreachable Error - HowTos
- How to make use of SQLite Module in Python? - Python
- How to turn off Dark Mode in Excel App for Mac - Windows
- How to show console in Eclipse IDE - Eclipse
- Fix Power BI 404 not found error when connecting SharePoint Online List as Data Source - SharePoint