我正在探索 TemporalQuery
和 TemporalAccessor
在 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
andTemporalAccessor
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
, andZoneDateTime
are some examples ofTemporalAccesor
.
接下来是示例代码(基于前一本书中的一些示例):
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/