As I was typing this up, I figured it out on my own. I'm going to answer my own question for future users including myself.
I'd like to be able to save to LocalDateTime objects in an interval, but it seems that it not allowed. When I do this:
LocalDateTime start = new LocalDateTime();
LocalDateTime end = start.plusMinutes( 30 );
Interval interval = new Interval( start, end );
I get compilation problems. I think it is because LocalDateTime is not a ReadableInstant.
Is there a suitable workaround?
It looks like this is possible:
Period period = new Period( start, end );
But then you lose the "getStart\End" methods.
In order to use Interval with LocalDateTime you have to do this:
Interval interval = new Interval( start.toDateTime(), end.toDateTime() );
The resulting time makes it easy to debug as the time will be converted from the local time zone and you know what time zone you are dealing with. However it will have issues during daylight savings time periods. To work around that coerce the result to DateTimeZone.UTC in which case you will lose the timezone data associated with the time (which you don't normally care about if you actually started with LocalDateTime, but make sure you use it consistently and never build your Intervals without the conversion.
Interval interval = new Interval(
start.toDateTime(DateTimeZone.UTC), end.toDateTime(DateTimeZone.UTC) );
This is demonstrated by the following test (assuming you are in EST time zone)
@Test
public void testLocalDateTimeDST() {
LocalDateTime dst_2017_03_12 = LocalDateTime.parse("2017-03-12T02:01:00");
System.out.println(dst_2017_03_12);
try {
System.out.println(dst_2017_03_12.toDateTime());
Assert.fail("Should've thrown an IllegalInstantException");
} catch (IllegalInstantException e) {
}
System.out.println(dst_2017_03_12.toDateTime(DateTimeZone.UTC));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With