I am having a hard time comparing two DateTime objects that are in different TimeZones.
What I already know:
1) I know that the "isBefore()" method does not take TimeZones into account. So, the "if" condition bellow is not true (even though I would like it to be true):
long aRandomTimeSinceTheEpoch = 1234567789L;
String TIMEZONE_SYDNEY_AUSTRALIA = "Australia/Sydney";
DateTimeZone sydneyTimeZone = DateTimeZone.forID(TIMEZONE_SYDNEY_AUSTRALIA);
Chronology chronologySydney = GJChronology.getInstance(sydneyTimeZone);
String TIMEZONE_NEWYORK = "America/New_York";
DateTimeZone newYorkTimeZone = DateTimeZone.forID(TIMEZONE_NEWYORK);
Chronology chronologyNewYork = GJChronology.getInstance(newYorkTimeZone);
DateTime sydneyDateTime = new DateTime(aRandomTimeSinceTheEpoch, chronologySydney);
DateTime newYorkDateTime = new DateTime(aRandomTimeSinceTheEpoch, chronologyNewYork);
if( newYorkDateTime.isBefore(sydneyDateTime) ){
System.out.println("true");
}
2) Based on this answer (https://stackoverflow.com/a/8793980) it seems the correct way to do it is by Period instead, as Period is the right concept for what I am trying to do. However, that code throws an "UnsupportedOperationException - if the period contains years or months" exception sometimes (because I am dealing with dates that can be up to 2 years away from each other).
In short, all I want is an "isBefore()" method that takes TimeZones into account. (and that does not throw exceptions like the one above). How can I achieve this in Joda?
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