Home > Java > javaTutorial > body text

What is a datetime field in Java?

王林
Release: 2023-09-04 14:41:06
forward
1338 people have browsed it

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);
   }
}
Copy after login

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
Copy after login

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);  
   }
}
Copy after login

Output

20:01:43.171
Year: 2019
Year: 8
Year: 3
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template