Joda Api handles timezone for isBeforeNow isAfterNow comparison

I want to check if a given DateTime matches before or after the current DateTime. I converted the input time and current time to a common time zone (e.g. UTC) and a DateTime comparison. But I came across Yoda Api, so I was curious to find out if Yoda is able to do this without converting the time zone. Example:

clientDateTime.isBeforeNow() 
+4
source share
1 answer

Yes, comparing Joda DateTime with current time does not require a time zone conversion.

When compared to the current time, such as DateTime.isBeforeNow and DateTime.isAfterNow , Joda simply compares the base absolute milliseconds from January 1, 1970. The same point in time has exactly the same absolute value of milliseconds, regardless of the time zone.

For example, the moment 1355625068295 corresponds to:

 DateTime dt = new DateTime(1355625068295L); DateTime utc = dt.withZone(DateTimeZone.UTC); DateTime ny = dt.withZone(DateTimeZone.forID("America/New_York")); DateTime tk = dt.withZone(DateTimeZone.forID("Asia/Tokyo")); System.out.println(utc.getMillis() + " is " + utc); System.out.println(ny.getMillis() + " is " + ny); System.out.println(tk.getMillis() + " is " + tk); 

Output:

 1355625068295 is 2012-12-16T02:31:08.295Z 1355625068295 is 2012-12-15T21:31:08.295-05:00 1355625068295 is 2012-12-16T11:31:08.295+09:00 

And when comparing with "now":

 System.out.println("now: " + new DateTime().getMillis()); System.out.println(ny.isBeforeNow()); System.out.println(ny.plusHours(1).isAfterNow()); System.out.println(tk.isBeforeNow()); System.out.println(tk.plusHours(1).isAfterNow()); 

Output:

 now: 1355625752323 true true true true 
+6
source

All Articles