Chris Brown
Chris Brown

Reputation: 35

DateFormat returning wrong hour

I am trying to parse the string "20/08/18 13:21:00:428" using the DateFormat class and a formatting pattern of "dd/MM/yy' 'HH:mm:ss:SSS". The Timezone is set to BST.

The date returned for the above is correct but the time is getting returned as 08 for the hours instead of 13 - "Mon Aug 20 08:21:00 BST 2018"

The following snippet prints the date and time just mentioned:

    String toBeParsed = "20/08/18 13:21:00:428";
    DateFormat format = new SimpleDateFormat("dd/MM/yy' 'HH:mm:ss:SSS");
    format.setTimeZone(TimeZone.getTimeZone("BST"));
    Date parsedDate = format.parse(toBeParsed);
    System.out.println(parsedDate);

Is this something to do with my timezone or have I misunderstood the pattern?

Upvotes: 0

Views: 1153

Answers (2)

Anonymous
Anonymous

Reputation: 86333

java.time

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/uu H:mm:ss:SSS");
    String toBeParsed = "20/08/18 13:21:00:428";
    ZonedDateTime dateTime = LocalDateTime.parse(toBeParsed, formatter)
            .atZone(ZoneId.of("Europe/London"));
    System.out.println(dateTime);

Output from this snippet is:

2018-08-20T13:21:00.428+01:00[Europe/London]

What went wrong in your code?

While I always recommend against the long outdated and poorly designed classes Date, TimeZone and DateFormat, in this case they are behaving particularly confusingly. Printing a Date on a JVM with Europe/London as default time zone gives time zone as BST if the date is in the summer time part of the year:

    TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
    Date oldFashionedDate = new Date();
    System.out.println(oldFashionedDate);

Mon Aug 20 15:45:39 BST 2018

However, when I give time zone as BST, Bangladesh time is understood, but it comes out with the non-standard abbreviation BDT:

    TimeZone.setDefault(TimeZone.getTimeZone("BST"));
    System.out.println(oldFashionedDate);

Mon Aug 20 20:45:39 BDT 2018

(I have observed this behaviour on Java 8 and Java 10.)

Another lesson to learn is never to rely on three and four letter time zone abbreviations. They are ambiguous and not standardized.

  • BST may mean Brazil Summer Time or Brazilian Summer Time, Bangladesh Standard Time, Bougainville Standard Time or British Summer Time (note that S is sometimes for Standard, sometimes for Summer, which is typically the opposite of Standard Time).
  • BDT may mean Brunei Darussalam Time or British Daylight Time (another name for British Summer Time (BST)), but I wasn’t aware that Bangladesh Time was also sometimes abbreviated this way.

PS Thanks to DodgyCodeException for spotting the time zone abbreviation interpretation issue.

Link

Time Zone Abbreviations — Worldwide List

Upvotes: 1

DodgyCodeException
DodgyCodeException

Reputation: 6123

BST is Bangladesh Standard Time. The correct time zone to use is "Europe/London" if you want automatic summer time, or "UTC+1" if you want British Summer Time always.

See https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html#SHORT_IDS

Upvotes: 4

Related Questions