- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is date and time temporal field in Java?
A temporal field is a field of date-time, such as month-of-year or hour-of-minute. These fields are represented by the TemporalField interface and the ChronoField class implements this interface.
The get() or getLong() methods of the class LocaldateTime accepts a temporal field as a parameter and gets the value of the given field in the current object.
Example
Following example retrieves the values of the date relates temporal fields from a date.
import java.time.LocalDateTime; import java.time.temporal.ChronoField; public class Demo { public static void main(String args[]) { //Instantiating the LocalDateTime class LocalDateTime lDate = LocalDateTime.now(); int field = lDate.get(ChronoField.DAY_OF_MONTH); System.out.println("Day of the month: "+field); field = lDate.get(ChronoField.DAY_OF_WEEK); System.out.println("Day of the month: "+field); field = lDate.get(ChronoField.DAY_OF_YEAR); System.out.println("Day of the month: "+field); long epoch = lDate.getLong(ChronoField.EPOCH_DAY); System.out.println("Day of the month: "+epoch); field = lDate.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH); System.out.println("Week in the month: "+field); field = lDate.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR); System.out.println("Day of the week in an year: "+field); field = lDate.get(ChronoField.ERA); System.out.println("Era: "+field); } }
Output
Day of the month: 25 Day of the month: 5 Day of the month: 237 Day of the month: 17403 Week in the month: 4 Day of the week in an year: 6 Am or Pm: 6 Era: 1
Example
Following example retrieves the values of the time relates temporal fields from a date.
import java.time.Clock; import java.time.LocalDateTime; import java.time.temporal.ChronoField; public class CreateDateTime { public static void main(String args[]) { //Instantiating the LocalDateTime class LocalDateTime lDateTime = LocalDateTime.now(); System.out.println(lDate); int field = lDateTime.get(ChronoField.CLOCK_HOUR_OF_AMPM); System.out.println("Hour of the day: "+field); field = lDateTime.get(ChronoField.AMPM_OF_DAY); System.out.println("Am or Pm: "+field); field = lDateTime.get(ChronoField.CLOCK_HOUR_OF_DAY); System.out.println("Hour of the day: "+field); long epoch = lDateTime.getLong(ChronoField.MINUTE_OF_DAY); System.out.println("Minute of the day: "+epoch); field = lDateTime.get(ChronoField.MINUTE_OF_HOUR); System.out.println("Minutes of the hour: "+field); field = lDateTime.get(ChronoField.SECOND_OF_DAY); System.out.println("Seconds of the day: "+field); field = lDateTime.get(ChronoField.SECOND_OF_MINUTE); System.out.println("Seconds of the minute: "+field); } }
Output
2020-11-11T14:58:22.680 Hour of the day: 2 Am or Pm: 1 Hour of the day: 14 Minute of the day: 898 Minutes of the hour: 58 Seconds of the day: 53902 Seconds of the minute: 22
- Related Articles
- What are date temporal fields in Java?
- What are time temporal fields in Java?
- What is Java reg ex to check for date and time?
- Add DATE and TIME fields to get DATETIME field in MySQL?
- Get Date and Time parts separately in Java
- Modify Date and Time from GregorianCalendar in Java
- What is the Temporal Data Mining?
- Java Program to parse date and time
- Java Program to display date and time in Milliseconds
- What is the difference between Spatial and Temporal Data Mining?
- Java Program to Display current Date and Time
- How to get Time in Milliseconds for the Given date and time in Java?
- Display complete date and time information using Formatter in Java
- Java Program to display date and time information in lowercase
- Java Program to display date and time information in uppercase

Advertisements