gpt4 book ai didi

java - 什么是 Java 中的时间对象?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:17:42 25 4
gpt4 key购买 nike

我正在探索 TemporalQueryTemporalAccessor在 Java 8 中引入。TemporalAccessor 似乎是专门为时间对象(例如日期、时间、偏移量或它们的某种组合)设计的。什么是临时对象?一些谷歌搜索给出了它的意思

An object that changes over time

但无法在 Java 的上下文中关联它?

最佳答案

根据 Joda-Time 和 JSR 310,问题、概念和方法 [PDF] , TemporalAccessor:

Defines read-only access to a temporal object, such as a date, time, offset or some combination of these

JSR-310 Date and Time API Guide状态:

Fields and units work together with the abstractions Temporal and TemporalAccessor provide access to date-time classes in a generic manner.

本书Beginning Java 8 Fundamentals: Language Syntax, Arrays, Data Types, Objects, and Regular Expressions说:

All classes in the API that specify some kind of date, time, or both are TemporalAccesor. LocalDate, LocalTime, LocalDateTime, and ZoneDateTime are some examples of TemporalAccesor.

接下来是示例代码(基于前一本书中的一些示例):

public static boolean isFriday13(TemporalAccessor ta) {
if (ta.isSupported(DAY_OF_MONTH) && ta.isSupported(DAY_OF_WEEK)) {
int dayOfMonth = ta.get(DAY_OF_MONTH);
int weekDay = ta.get(DAY_OF_WEEK);
DayOfWeek dayOfWeek = DayOfWeek.of(weekDay);
if (dayOfMonth == 13 && dayOfWeek == FRIDAY) {
return true;
}
}
return false;
}

public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
TemporalAccessor ta = formatter.parse("02/13/2015");
LocalDate ld = LocalDate.from(ta);
System.out.println(ld);
System.out.println(isFriday13(ld));
}

关于java - 什么是 Java 中的时间对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28229721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com