- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 are date temporal fields 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.
Following are the list of various temporal fields regarding date supported by the ChronoField class −
Field | Description |
---|---|
ALIGNED_DAY_OF_WEEK_IN_MONTH | This field represents the day of the week with in a month. |
ALIGNED_DAY_OF_WEEK_IN_YEAR | This field represents the aligned day of a week in an year. |
ALIGNED_WEEK_OF_MONTH | This field represents the aligned wee of a month. |
ALIGNED_WEEK_OF_YEAR | This field represents the aligned week of an year. |
DAY_OF_MONTH | This field represents the day of a month. |
DAY_OF_WEEK | This field represents the day of a week. |
DAY_OF_YEAR | This field represents the day of a year. |
EPOCH_DAY | This field represents the epoch day of an year. |
ERA | This field represents the era of the year. |
YEAR | This field represents the year. |
YEAR_OF_ERA | This field represents the year of the era. |
The get() or getLong() methods of the classes LocalDate and LocaldateTime accepts a temporal field as a parameter and gets the value of the given field in the current object.
Example
import java.time.LocalDate; import java.time.temporal.ChronoField; public class Demo { public static void main(String args[]) { //Instantiating the LocalDate class LocalDate lDate = LocalDate.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: 11 Day of the month: 3 Day of the month: 316 Day of the month: 18577 Week in the month: 4 Day of the week in an year: 1 Era: 1
Example
import java.time.DayOfWeek; import java.time.LocalTime; import java.time.Month; import java.time.Year; import java.time.temporal.ChronoField; public class Demo { public static void main(String args[]) { //Instantiating the LocalDateTime class LocalTime lTime = LocalTime.now(); System.out.println(lTime); int field = Year.of(2019).get(ChronoField.YEAR); System.out.println("Year: "+field); field = Month.of(8).get(ChronoField.MONTH_OF_YEAR); System.out.println("Year: "+field); field = DayOfWeek.of(3).get(ChronoField.DAY_OF_WEEK); System.out.println("Year: "+field); } }
Output
20:01:43.171 Year: 2019 Year: 8 Year: 3
Advertisements