Search code examples
javatimehourdatetime-parsing

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?


Solution

  • 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