Computer >> Máy Tính >  >> Lập trình >> Java

Các trường thời gian ngày tháng trong Java là gì?

Trường thời gian là trường ngày-giờ, chẳng hạn như tháng-năm hoặc giờ-phút. Các trường này được đại diện bởi giao diện TemporalField và lớp ChronoField triển khai giao diện này.

Sau đây là danh sách các trường thời gian khác nhau liên quan đến ngày tháng được hỗ trợ bởi lớp ChronoField -

Field Mô tả
ALIGNED_DAY_OF_WEEK_IN_MONTH
Trường này đại diện cho ngày trong tuần với trong một tháng.
ALIGNED_DAY_OF_WEEK_IN_YEAR
Trường này đại diện cho ngày được căn chỉnh của một tuần trong một năm.
ALIGNED_WEEK_OF_MONTH
Trường này đại diện cho một tháng được căn chỉnh.
ALIGNED_WEEK_OF_YEAR
Trường này đại diện cho tuần được căn chỉnh trong năm.
DAY_OF_MONTH
Trường này đại diện cho ngày trong tháng.
DAY_OF_WEEK
Trường này đại diện cho ngày trong tuần.
DAY_OF_YEAR
Trường này đại diện cho ngày trong năm.
EPOCH_DAY
Trường này đại diện cho ngày kỷ nguyên trong năm.
ERA
Trường này đại diện cho thời đại trong năm.
NĂM
Trường này đại diện cho năm.
YEAR_OF_ERA
Trường này đại diện cho năm của thời đại.

Các phương thức get () hoặc getLong () của các lớp LocalDate và LocaldateTime chấp nhận một trường tạm thời làm tham số và nhận giá trị của trường đã cho trong đối tượng hiện tại.

Ví dụ

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);
   }
}

Đầu ra

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

Ví dụ

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);  
   }
}

Đầu ra

20:01:43.171
Year: 2019
Year: 8
Year: 3