1

I'm trying to parse a date with three Locales: UK, US, ENGLISH.

 String s = "Mon Sep 12 00:02:16 BST 2022";
 DateTimeFormatter formatter = null;
 String pattern = "E MMM dd H:m:s z yyyy";
 formatter = DateTimeFormatter.ofPattern(pattern).withLocale(Locale.UK);
 System.out.println("UK: " + Instant.from(formatter.parse(s)));
 formatter = DateTimeFormatter.ofPattern(pattern).withLocale(Locale.US);
 System.out.println("US: " + Instant.from(formatter.parse(s)));
 formatter = DateTimeFormatter.ofPattern(pattern).withLocale(Locale.ENGLISH);
 System.out.println("EN: " + Instant.from(formatter.parse(s)));

Result:

UK: 2022-09-11T23:02:16Z
US: 2022-09-11T13:02:16Z
EN: 2022-09-11T13:02:16Z

UK is correct. Both US and ENGLISH are wrong. Why?

Edit : test was made on Windows 10. openjdk 11.

grigouille
  • 511
  • 3
  • 14
  • 1
    Locale in this instance means "parse the text according to the language conventions of that country", not "parse the text according to the timezone of that country". The zone in all 3 cases is BST - British Summer Time. You might, for example, want to parse a date written in French (Monday = Lundi) representing a date in a British timezone. – Michael Sep 12 '22 at 09:20
  • 1
    @Michael The US and EN outputs show a *different* time (1pm instead of 11pm). – Konrad Rudolph Sep 12 '22 at 09:22
  • What is the "correct" time zone of US?? ;) ..And what is the "correct" time zone of "English"? – xerx593 Sep 12 '22 at 09:22
  • @KonradRudolph Missed it, but cannot reproduce. On my machine, jdk8.0.275, the output is the same in all 3 cases. – Michael Sep 12 '22 at 09:23
  • ..answer: it is ambiguous – xerx593 Sep 12 '22 at 09:23
  • ..whereas "UK" covers one single time zone!(?) – xerx593 Sep 12 '22 at 09:24
  • @xerx593 See my comment further up. – Konrad Rudolph Sep 12 '22 at 09:25
  • 3
    Strange: if I run this on JDK 17, the first case fails with a parse exception. For UK, the parser for some reason expects "Sept" instead of "Sep" (apparently that is how the British abbreviate September). But when I fix that, it returns the same result for all three cases. – Jesper Sep 12 '22 at 09:25
  • 1
    @Jesper https://stackoverflow.com/questions/69267710/septembers-short-form-sep-no-longer-parses-in-java-17-in-en-gb-locale/69268271#69268271 – Michael Sep 12 '22 at 09:28
  • 2
    Suspect your problem is that BST is parsing to Bourgainville Standard Time, not British Standard Time. "BST" is ambiguous. Cannot account for the output being different between the 3 cases. I'm assuming a transcription error or something. Doesn't seem possible. `ZonedDateTime.from(formatter.parse(s)).getZone() = Pacific/Bougainville` – Michael Sep 12 '22 at 09:35
  • BST may, as a third possibility, be parsed as Bangladesh Standard Time. I don’t know whether there are still more possibilities. – Ole V.V. Sep 13 '22 at 15:33

0 Answers0