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
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP