Java 中的日期時間欄位是什麼?
時間欄位是日期時間的欄位,例如一年中的月份或一分鐘中的小時。這些欄位由 TemporalField 介面表示,ChronoField 類實現了此介面。
以下是 ChronoField 類支援的關於日期的各種時間欄位的列表:
| 欄位 | 描述 |
|---|---|
| ALIGNED_DAY_OF_WEEK_IN_MONTH | 此欄位表示一個月中的星期幾。 |
| ALIGNED_DAY_OF_WEEK_IN_YEAR | 此欄位表示一年中對齊的星期幾。 |
| ALIGNED_WEEK_OF_MONTH | 此欄位表示一個月中對齊的星期。 |
| ALIGNED_WEEK_OF_YEAR | 此欄位表示一年中對齊的星期。 |
| DAY_OF_MONTH | 此欄位表示一個月中的某一天。 |
| DAY_OF_WEEK | 此欄位表示一週中的某一天。 |
| DAY_OF_YEAR | 此欄位表示一年中的某一天。 |
| EPOCH_DAY | 此欄位表示一年的紀元日。 |
| ERA | 此欄位表示年的紀元。 |
| YEAR | 此欄位表示年份。 |
| YEAR_OF_ERA | 此欄位表示紀元的年份。 |
LocalDate 和 LocaldateTime 類的 get() 或 getLong() 方法接受時間欄位作為引數,並獲取當前物件中給定欄位的值。
示例
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);
}
}輸出
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
示例
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);
}
}輸出
20:01:43.171 Year: 2019 Year: 8 Year: 3
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP