Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the timezone pattern "OOOO" not show the full GMT+00:00 offset format?

Is this a bug or a feature?

The DateTimeFormatter JavaDoc explicitly states that when I use the OOOO pattern in my formatter, the full form of localized timezone should be used (emphasis mine):

Four letters outputs the full form, which is localized offset text, such as 'GMT, with 2-digit hour and minute field, optional second field if non-zero, and colon, for example 'GMT+08:00'.

But in case the time is in GMT+0:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE yyyy.MM.dd HH:mm:ss.SSS OOOO");

String timestamp = OffsetDateTime.ofInstant(Instant.now(), ZoneOffset.UTC).format(formatter);
System.out.println(timestamp);

This is the output:

Mon 2019.02.25 22:30:00.586 GMT

Expected:

Mon 2019.02.25 22:30:00.586 GMT+00:00
like image 769
Snackoverflow Avatar asked Oct 17 '22 06:10

Snackoverflow


1 Answers

A bug? We seem to agree that the observed behaviour does not agree with the documentation (or at least you will have to do a very creative reading of the documentation to make it match).

A feature? As far as I can tell the observed behaviour is a conscious decision at some point. The source code for the private inner class LocalizedOffsetIdPrinterParser inside DateTimeFormatterBuilder contains if (totalSecs != 0) { before printing hours, minutes and seconds. It doesn’t look like a copy-paste error since the exact same code line is nowhere else in the file (offset 0 is treated specially in a number of places, but I am not aware of anywhere else it is left out completely).

On Java 8 format pattern OOOO neither parses GMT alone nor GMT+00:00, which must be a bug. It’s fixed in Java 11. On Java 11 OOOO parses GMT alone just fine, so they must have considered this acceptable (it parses GMT+00:00 and GMT-00:00 too, though).

You may consider filing a bug with Oracle and/or OpenJDK (I’m unsure about the right place these days). Whether they will reject it, fix the documentation or fix the code — I dare not try to guess.

Workaround: 'GMT'xxx

Anyway, I want my +00:00 somehow.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE yyyy.MM.dd HH:mm:ss.SSS 'GMT'xxx");

Wed 2019.02.27 08:46:43.226 GMT+00:00

like image 145
Ole V.V. Avatar answered Oct 29 '22 18:10

Ole V.V.