
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,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- How to Change AWS Default Region using CLI - AWS
- Mac OS X Error: Could not create the Java Virtual Machine - Mac-OS-X
- cURL -x Use of HTTP Proxy Server Examples - cURL
- Java Join Strings with Comma Separator - Java
- Fix: AttributeError: str object has no attribute decode. Did you mean: encode?[Python] - Python
- Fix Maven: Could not find artifact in central - Java
- [IRCTC] Indian railways official eRail API 1.1 for developers to get train info - HowTos
- Define an Infinite Number in Python - Python