What are time temporal fields in Java?

JavaServer Side ProgrammingProgramming

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 time supported by the ChronoField class −

FieldDescription
CLOCK_HOUR_OF_AMPM
This field represents the clock hour of a day (am/pm).
AMPM_OF_DAY
This field represents the ap/pm of the day.
CLOCK_HOUR_OF_DAY
This field represents the clock hour of a day.
HOUR_OF_AMPM
This field represents the hour of a day (am/pm).
HOUR_OF_DAY
This field represents the hour of a day.
INSTANT_SECONDS
This field represents the instant epoch seconds.
MICRO_OF_DAY
This field represents the micro of a day.
MICRO_OF_SECOND
This field represents the micro of a second.
MILLI_OF_DAY
This field represents the milli of a day.
MILLI_OF_SECOND
This field represents the milli of a second.
MINUTE_OF_DAY
This field represents the minute of the day.
MINUTE_OF_HOUR
This field represents the hour of the day.
MONTH_OF_YEAR
This field represents the month of the year.
NANO_OF_DAY
This field represents the nano of the day.
NANO_OF_SECOND
This field represents the nano of the second.
OFFSET_SECONDS
This field represents the offset from UTC/Greenwich.
PROLEPTIC_MONTH
This field represents the proleptic-month.
SECOND_OF_DAY
This field represents the second of the day.
SECOND_OF_MINUTE
This field represents the second of the minute.

The get() or getLong() methods of the classes LocalDate accepts a temporal field as a parameter and gets the value of the given field in the current object.

Example

Live Demo

import java.time.LocalTime;
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 = lTime.get(ChronoField.CLOCK_HOUR_OF_AMPM);
      System.out.println("Hour of the day: "+field);
      field = lTime.get(ChronoField.AMPM_OF_DAY);
      System.out.println("Am or Pm: "+field);      
      field = lTime.get(ChronoField.CLOCK_HOUR_OF_DAY);
      System.out.println("Hour of the day: "+field);
      long epoch = lTime.getLong(ChronoField.MINUTE_OF_DAY);
      System.out.println("Minute of the day: "+epoch);
      field = lTime.get(ChronoField.MINUTE_OF_HOUR);
      System.out.println("Minutes of the hour: "+field);
      field = lTime.get(ChronoField.SECOND_OF_DAY);
      System.out.println("Seconds of the day: "+field);
      field = lTime.get(ChronoField.SECOND_OF_MINUTE);
      System.out.println("Seconds of the minute: "+field);
   }
}

Output

17:02:46.294
Hour of the day: 5
Am or Pm: 1
Hour of the day: 17
Minute of the day: 1022
Minutes of the hour: 2
Seconds of the day: 61366
Seconds of the minute: 46
raja
Published on 06-Feb-2021 04:21:12
Advertisements