Thursday 15 April 2010

jodatime - Joda: How to compare date instances in same time zone with different time zone representations? -


I am trying to compare two Joda dates for equality in my unit tests. Expected is built with new date time (...) while has been parsed from that string to dateTimeFormatter is parsed with.

Although my date string is the timezone slice +02: 00 , the date parsed is the time zone Europe / Berlin . This semant is correct (at least during DST), although two dates are not identical .

I can definitely make my expected time zone with DateTimeZone. ForID ("Europe / Berlin") , but it will not be completely accurate IMHO +01: 00 during the winter in Europe / Berlin and < Code> +02: 00 Input is offset during the summer, the input string clearly states +02: 00 , so if the input unit is in winter, my unit test will fail.

Any ideas about fixing it?

Example Code

  DateTime Formatter = ISODateTimeFormat.dateTime (); Date Time Expected = New Date Time (2013, 6, 22, 11, 7, 22, 123, DateTime Offset (2)); String input = "2013-06-22T11: 07: 22.123 + 02: 00"; Date time pars = format Parsed time; System.out.println ("Expected:" + Expected + "(" + Expected.Gronronology () + ")"); System.out.println ("Parsed:" + Parsed + "(" + Parsed.GatRrnology (+) + ");); System.out.println ("Similar:" + Expected.Accles (Parsed)); Expected output   
 ]: 00]) Parsed: 2013-06-22T11: 07: 22.123 + 02: 00 (isogronology [Europe / Berlin]) Equal: Incorrect    

Just got the answer itself. There is an option for DateTimeFormatter which prevents it from resolving time zone offset:

  ISODateTimeFormat.dateTime (). WithOffsetParsed ()    

No comments:

Post a Comment