Search code examples
javatimezonejava-timedatetime-parsingzoneddatetime

Timezone BST has wrong offset


Cannot understand why BST (UTC Offset: UTC +1) got offset +11 in following code. I would expect +1.

final String lastUpdated = "Mon Sep 27 18:29:00 BST 2021";
final DateTimeFormatter LAST_UPDATED_FORMATTER = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z y").withLocale(Locale.UK);
ZonedDateTime zonedDateTime = ZonedDateTime.parse(lastUpdated, LAST_UPDATED_FORMATTER);
LocalDateTime localDateTime = zonedDateTime.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime();
System.out.println(zonedDateTime.getOffset()); // +11:00; expected +1
System.out.println(localDateTime.toString());

Console output:

+11:00
2021-09-27T07:29

All is clear once is date in UTC:

final String lastUpdated = "Mon Sep 27 18:29:00 BST 2021";

Solution

  • That's what you get for using ambiguous abbreviations, I'm afraid. BST can mean different things, as shown here.

    It looks like in this case it's being interpreted as Bougainville Standard Time, which is indeed UTC+11.

    (Even within the UK, BST hasn't always meant British Summer Time. Between 1968 and 1971, BST meant "British Standard Time", although it was still GMT+1.)

    Using abbreviations is "somewhat okay" for formatting, but should be avoided for parsing purposes where at all possible. If you're receiving data that you know will use it, and you know what it should mean, you're probably best off replacing it in the text before parsing.