
时间字段是日期时间字段,例如一年中的月份或分钟中的小时。这些字段由 TemporalField 接口表示,ChronoField 类实现该接口。
以下是 ChronoField 类支持的有关日期的各种时间字段的列表 -
| 字段 | 描述 |
|---|---|
| ALIGNED_DAY_OF_WEEK_IN_MONTH | 该字段表示一个月中的星期几。 |
| 此字段表示一年中一周的对齐日期。 | |
| ALIGNED_WEEK_OF_MONTH | 此字段表示一个月的对齐周。 |
| ALIGNED_WEEK_OF_YEAR | 此字段表示对齐的周年。 |
| DAY_OF_MONTH | 此字段代表一个月中的第几天。 |
| DAY_OF_WEEK | 该字段代表一周中的某一天。 |
| DAY_OF_YEAR | 此字段代表一年中的第几天。 |
| EPOCH_DAY | 该字段代表一年中的纪元日。 |
| ERA | 该字段代表当年的时代。 |
| 年份 | 该字段代表年份。 |
| 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中的日期时间字段?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号