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
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
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