Temporary request
The java.time structure includes an architecture for querying a date and time value: Temporal Query . Some implementations of the TemporalQuery interface can be found in the plural name TemporalQueries .
You can also write your own implementation. TemporalQuery is a functional interface that is, it has a single method declared. queryFrom method .
This is my first attempt at implementing TemporalQuery , so grab it with salt. Here is the full class. Free ( ISC License ), but completely at your own peril and risk.
The tricky part is the “Questions” requirement, that the weekend is determined by UTC, not the time zone or offset of the past date and time value. Therefore, we need to adjust the past date-time value in UTC format. Although Instant logically equivalent, I used OffsetDateTime with a UTC offset since it is more flexible. In particular, OffsetDateTime offers the getDayOfWeek method.
CAVEAT: I have no idea that I am doing something in the orthodox method, since I do not fully understand the basics of java.times projects, as provided by its creators. In particular, I'm not sure that my casting TemporalAccessor ta in java.time.chrono.ChronoZonedDateTime is correct. But it seems to work quite well.
It would be better if this class worked with Instant instances, as well as ChronoZonedDateTime / ZonedDateTime .
package com.example.javatimestuff; import java.time.LocalDate; import java.time.LocalTime; import java.time.ZoneId; import java.time.ZonedDateTime; public class WeekendFri2200ToSun2300UtcQuery implements TemporalQuery<Boolean> { static private final EnumSet<DayOfWeek> WEEKEND_DAYS = EnumSet.of ( DayOfWeek.FRIDAY , DayOfWeek.SATURDAY , DayOfWeek.SUNDAY ); static private final OffsetTime START_OFFSET_TIME = OffsetTime.of ( LocalTime.of ( 22 , 0 ) , ZoneOffset.UTC ); static private final OffsetTime STOP_OFFSET_TIME = OffsetTime.of ( LocalTime.of ( 23 , 0 ) , ZoneOffset.UTC ); @Override public Boolean queryFrom ( TemporalAccessor ta ) { if ( ! ( ta instanceof java.time.chrono.ChronoZonedDateTime ) ) { throw new IllegalArgumentException ( "Expected a java.time.chrono.ChronoZonedDateTime such as `ZonedDateTime`. Message # b4a9d0f1-7dea-4125-b68a-509b32bf8d2d." ); } java.time.chrono.ChronoZonedDateTime czdt = ( java.time.chrono.ChronoZonedDateTime ) ta; Instant instant = czdt.toInstant (); OffsetDateTime odt = OffsetDateTime.ofInstant ( instant , ZoneOffset.UTC ); DayOfWeek dayOfWeek = odt.getDayOfWeek (); if ( ! WeekendFri2200ToSun2300UtcQuery.WEEKEND_DAYS.contains ( dayOfWeek ) ) {
Use this TemporalQuery . Although defining TemporalQuery requires some work, using it is so simple and simple:
Using.
WeekendFri2200ToSun2300UtcQuery query = new WeekendFri2200ToSun2300UtcQuery ();
I added a static description method for debugging and logging to check query settings. This is my own method that is not required by the TemporalQuery interface.
System.out.println ( "Weekend is: " + WeekendFri2200ToSun2300UtcQuery.description () );
First today, Tuesday. Should not be at the weekend.
ZonedDateTime now = ZonedDateTime.now ( ZoneId.of ( "America/Montreal" ) ); Boolean nowIsWithinWeekend = now.query ( query ); System.out.println ( "now: " + now + " is in weekend: " + nowIsWithinWeekend );
Now this Friday morning. Should not be at the weekend.
ZonedDateTime friday1000 = ZonedDateTime.of ( LocalDate.of ( 2016 , 4 , 29 ) , LocalTime.of ( 10 , 0 ) , ZoneId.of ( "America/Montreal" ) ); Boolean friday1000IsWithinWeekend = friday1000.query ( query ); System.out.println ( "friday1000: " + friday1000 + " is in weekend: " + friday1000IsWithinWeekend );
And late this Friday. Must be TRUE on the weekend.
ZonedDateTime friday2330 = ZonedDateTime.of ( LocalDate.of ( 2016 , 4 , 29 ) , LocalTime.of ( 23 , 30 ) , ZoneId.of ( "America/Montreal" ) ); Boolean friday2330IsWithinWeekend = friday2330.query ( query ); System.out.println ( "friday2330: " + friday2330 + " is in weekend: " + friday2330IsWithinWeekend );
At startup.
Weekend: WeekendFri2200ToSun2300UtcQuery {22: 00Z | [FRIDAY, SATURDAY, SUNDAY] | 23: 00Z}
Now: 2016-04-26T20: 35: 01.014-04: 00 [America / Montreal] is on the weekend: false
friday1000: 2016-04-29T10: 00-04: 00 [America / Montreal] is on the weekend: false
friday2330: 2016-04-29T23: 30-04: 00 [America / Montreal] is on the weekend: true
Local… does not mean local
Referring to the question ... saying that you want to compare the values of LocalDateTime with the values in UTC (start / end of the weekend) does not make sense. A LocalDateTime does not have an offset-from-UTC time zone. Although naming can be intuitive, the Local… classes mean that they can be applied to any locality that lacks locality. Thus, they have no meaning, they are not a point on the timeline until you apply the specified offset or time zone.
This whole answer suggests that you were confused by this terminology and intended to compare the actual moment on the timeline.