Home >Java >javaTutorial >What is a datetime field in Java?

What is a datetime field in Java?

王林
王林forward
2023-09-04 14:41:061398browse

What is a datetime field in Java?

A time field is a datetime field, such as a month in a year or an hour in a minute. These fields are represented by the TemporalField interface, which the ChronoField class implements.

The following is a list of various time fields related to dates supported by the ChronoField class -

ALIGNED_DAY_OF_WEEK_IN_YEAR

Field Description
ALIGNED_DAY_OF_WEEK_IN_MONTH

This field represents the day of the week in the month.

This field represents the aligned day of the week of the year.

ALIGNED_WEEK_OF_MONTH

This field represents the aligned week of the month.

ALIGNED_WEEK_OF_YEAR

This field represents the aligned anniversary.

DAY_OF_MONTH

This field represents the day of the month.

DAY_OF_WEEK

This field represents the day of the week.

DAY_OF_YEAR

This field represents the day of the year.

EPOCH_DAY

This field represents the epoch day of the year.

ERA

This field represents the era of the current 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 LocalDate and LocaldateTime classes accept the time field as a parameter and get the value of the given field in the current object .

Example

Live Demonstration

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

Live Demonstration

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

The above is the detailed content of What is a datetime field in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete