Computer >> Computer tutorials >  >> Programming >> Java

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