Java中的時間欄位是什麼?


時間欄位是日期時間欄位,例如一年中的月份或一分鐘中的小時。這些欄位由`TemporalField`介面表示,`ChronoField`類實現了此介面。

以下是`ChronoField`類支援的各種關於時間的時間欄位列表:

欄位描述
CLOCK_HOUR_OF_AMPM
此欄位表示一天中的時鐘小時(上午/下午)。
AMPM_OF_DAY
此欄位表示一天中的上午/下午。
CLOCK_HOUR_OF_DAY
此欄位表示一天中的時鐘小時。
HOUR_OF_AMPM
此欄位表示一天中的小時(上午/下午)。
HOUR_OF_DAY
此欄位表示一天中的小時。
INSTANT_SECONDS
此欄位表示瞬時紀元秒。
MICRO_OF_DAY
此欄位表示一天中的微秒。
MICRO_OF_SECOND
此欄位表示一秒中的微秒。
MILLI_OF_DAY
此欄位表示一天中的毫秒。
MILLI_OF_SECOND
此欄位表示一秒中的毫秒。
MINUTE_OF_DAY
此欄位表示一天中的分鐘。
MINUTE_OF_HOUR
此欄位表示一天中的小時。(應為一小時中的分鐘)
MONTH_OF_YEAR
此欄位表示一年中的月份。
NANO_OF_DAY
此欄位表示一天中的納秒。
NANO_OF_SECOND
此欄位表示一秒中的納秒。
OFFSET_SECONDS
此欄位表示與UTC/格林威治的偏移量。
PROLEPTIC_MONTH
此欄位表示前推月。
SECOND_OF_DAY
此欄位表示一天中的秒。
SECOND_OF_MINUTE
此欄位表示一分鐘中的秒。

`LocalDate`類的`get()`或`getLong()`方法接受時間欄位作為引數,並獲取當前物件中給定欄位的值。

示例

線上演示

import java.time.LocalTime;
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 = lTime.get(ChronoField.CLOCK_HOUR_OF_AMPM);
      System.out.println("Hour of the day: "+field);
      field = lTime.get(ChronoField.AMPM_OF_DAY);
      System.out.println("Am or Pm: "+field);      
      field = lTime.get(ChronoField.CLOCK_HOUR_OF_DAY);
      System.out.println("Hour of the day: "+field);
      long epoch = lTime.getLong(ChronoField.MINUTE_OF_DAY);
      System.out.println("Minute of the day: "+epoch);
      field = lTime.get(ChronoField.MINUTE_OF_HOUR);
      System.out.println("Minutes of the hour: "+field);
      field = lTime.get(ChronoField.SECOND_OF_DAY);
      System.out.println("Seconds of the day: "+field);
      field = lTime.get(ChronoField.SECOND_OF_MINUTE);
      System.out.println("Seconds of the minute: "+field);
   }
}

輸出

17:02:46.294
Hour of the day: 5
Am or Pm: 1
Hour of the day: 17
Minute of the day: 1022
Minutes of the hour: 2
Seconds of the day: 61366
Seconds of the minute: 46

更新於:2021年2月6日

752 次檢視

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.