The following correctly parses the text input "2022-12-29 01:16:03 GMT 08:00".
public ZonedDateTime parseZonedDateTime(String timeStr) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss O");
ZonedDateTime zonedDateTime = ZonedDateTime.parse(timeStr, dtf);
return zonedDateTime;
}
However, if the input string is "2022-12-29 01:16:03 UTC-08:00", ZonedDateTime.parse() method throws
java.time.format.DateTimeParseException: Text '2022-12-29 01:16:03 UTC-08:00' could not be parsed at index 20
According to the DateTimeFormatter doc, https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
both "GMT 08:00" and "UTC-08:00" are considered localized zone-offset 'O'.
What should be the correct pattern so that the parser accepts both GMT and UTC inputs?
I have tried using the format "yyyy-MM-dd HH:mm:ss OOOO" but the behavior is the same.
There is a similar question Why does `GMT 8` fail to parse with pattern `O` despite being copied straight out of doc? and according to this, the bug was fixed in Java 9 build b116.
However I'm using java 17.0.4.1-librca
CodePudding user response:
What should be the correct pattern so that the parser accepts both GMT and UTC inputs?
Use V
instead of O
i.e. use the pattern, yyyy-MM-dd HH:mm:ss VV
.
Demo:
public class Main {
public static void main(String args[]) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss VV", Locale.ENGLISH);
Stream.of(
"2022-12-29 01:16:03 GMT 08:00",
"2022-12-29 01:16:03 UTC-08:00"
)
.map(s -> ZonedDateTime.parse(s, dtf))
.forEach(System.out::println);
;
}
}
Output:
2022-12-29T01:16:03 08:00[GMT 08:00]
2022-12-29T01:16:03-08:00[UTC-08:00]
Learn more about the modern Date-Time API from Trail: Date Time.