I was exploring TemporalQuery and TemporalAccessor introduced in Java 8. TemporalAccessor seems to be specially designed for temporal objects such as date, time, offset or some combination of these . What are temporal objects?
Some googling gave its meaning as
An object that changes over time
But not able to relate this in the context of Java?
解决方案
According to with Joda-Time & JSR 310, Problems, Concepts and Approaches [PDF], the TemporalAccessor:
Defines read-only access to a temporal object,
such as a date, time, offset or some combination
of these
Fields and units work together with the abstractions Temporal and TemporalAccessor provide access to date-time classes in a generic manner.
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.
The next is a example code (based from some examples in the previous book):
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));
}