I have to input the date time in this format :
"2018-07-17T12:16:50.52Z"
Hence I am using :
private static final SimpleDateFormat LIVETRACK_DATE_TIME_FORMATTER =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
DATE_TIME_FORMATTER.parse(timeFrameFrom);
But I'm getting the below error when I run test case :
Tue Jul 17 1[2:20:50 CES]T 2018> but was:<Tue Jul 17 1[0:20:50 GM]T 2018>
Does that mean I should convert the date to GMT format? How do I convert it?
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"EEE MMM dd HH:mm:ss zzz yyyy 'Uhr'", Locale.ROOT);
String input = "2018-07-17T12:16:50.52Z";
ZonedDateTime dateTime = Instant.parse(input)
.atZone(ZoneId.of("Europe/Berlin"));
String output = dateTime.format(formatter);
System.out.println(output);
Output:
Tue Jul 17 14:16:50 CEST 2018 Uhr
Your test case is expecting the wrong result. The Z in your input string means UTC (offset zero from UTC or the so-called Zulu time zone). The time of 12:16:50.52 in UTC is the same as 14:16:50.52 in Central European Summer Time (CEST). So requiring Tue Jul 17 12:20:50 CEST 2018 Uhr is wrong.
In your code you hardcoded Z as a literal rather than parsing it as an offset as you should. Funnily this produced the wrong point in time that the unit test expected, only not in the expected time zone. This may have to do with Date.toString using the time zone setting of your JVM and the test expecting this to be CEST, when it really was GMT. However your SimpleDateFormat didn’t use GMT for parsing the string (probably CEST), so you haven’t given us enough information to explain everything that has happened in your test. SimpleDateFormat would use the same JVM setting unless you have explicitly set its time zone, which doesn’t happen in the code you have posted.
Link: Oracle tutorial: Date Time explaining how to use java.time.
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