There is simply no pattern (or for that matter, a configuration through DateTimeFormatterBuilder) that will parse "+HHmm" format time zones and all the textual zones specified in RFC-822.
Even the provided DateTimeFormatter.RFC_1123_DATE_TIME is specially constructed to only recognize "GMT". I have encountered lots of RFC-1123 timestamps that use "UT" instead, though. In any case, RFC-1123 specifically says:
"There is a strong trend towards the use of numeric timezone indicators, and implementations SHOULD use numeric timezones instead of timezone names. However, all implementations MUST accept either notation. If timezone names are used, they MUST be exactly as defined in RFC-822."
There's even a long standing comment in the code constructing RFC_1123_DATE_TIME saying:
// should handle UT/Z/EST/EDT/CST/CDT/MST/MDT/PST/MDT
The comment is present since Java 8. It's supposedly time to implement this? SimpleDateFormat "z" pattern was able to parse both +HHmm and all the time zone codes (except the military ones.)
Manually constructing a DateTimeFormatter with "z" also won't work as it only recognizes +HH:mm format (with colon.) "Z" pattern parses the colon-less +HHmm format, but on the other hand won't parse any of the textual zones declared in RFC-822 https://www.rfc-editor.org/rfc/rfc822#section-5.1 (to which RFC-1123 defers.)
Here's some reproducers:
import java.time.format.DateTimeFormatter;
public class Z1 {
public static void main(String[] args) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("d MMM yyyy HH:mm z");
df.parse("1 Oct 2022 08:59 +0000");
}
}
throws
java.time.format.DateTimeParseException: Text '1 Oct 2022 08:59 +0000' could not be parsed at index 17
import java.time.format.DateTimeFormatter;
public class Z2 {
public static void main(String[] args) {
DateTimeFormatter.RFC_1123_DATE_TIME.parse("1 Oct 2022 08:59 UT");
}
}
throws
java.time.format.DateTimeParseException: Text '1 Oct 2022 08:59 UT' could not be parsed at index 17
Even the provided DateTimeFormatter.RFC_1123_DATE_TIME is specially constructed to only recognize "GMT". I have encountered lots of RFC-1123 timestamps that use "UT" instead, though. In any case, RFC-1123 specifically says:
"There is a strong trend towards the use of numeric timezone indicators, and implementations SHOULD use numeric timezones instead of timezone names. However, all implementations MUST accept either notation. If timezone names are used, they MUST be exactly as defined in RFC-822."
There's even a long standing comment in the code constructing RFC_1123_DATE_TIME saying:
// should handle UT/Z/EST/EDT/CST/CDT/MST/MDT/PST/MDT
The comment is present since Java 8. It's supposedly time to implement this? SimpleDateFormat "z" pattern was able to parse both +HHmm and all the time zone codes (except the military ones.)
Manually constructing a DateTimeFormatter with "z" also won't work as it only recognizes +HH:mm format (with colon.) "Z" pattern parses the colon-less +HHmm format, but on the other hand won't parse any of the textual zones declared in RFC-822 https://www.rfc-editor.org/rfc/rfc822#section-5.1 (to which RFC-1123 defers.)
Here's some reproducers:
import java.time.format.DateTimeFormatter;
public class Z1 {
public static void main(String[] args) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("d MMM yyyy HH:mm z");
df.parse("1 Oct 2022 08:59 +0000");
}
}
throws
java.time.format.DateTimeParseException: Text '1 Oct 2022 08:59 +0000' could not be parsed at index 17
import java.time.format.DateTimeFormatter;
public class Z2 {
public static void main(String[] args) {
DateTimeFormatter.RFC_1123_DATE_TIME.parse("1 Oct 2022 08:59 UT");
}
}
throws
java.time.format.DateTimeParseException: Text '1 Oct 2022 08:59 UT' could not be parsed at index 17