Java8 time library does not interpret BST timezone correctly

Libby Shen :

This is not the dup of another timezone question because I do not have the luxury to choose the timezone format. I am in the process of migrating my java code to use the new java8 time library, but I found that the DateTimeFormatter does not interpret the time zone "BST" (British Summer Time) correctly. Instead of making it UTC+0100, it converted it to Pacific/Bougainville timezone. Anybody knows how I can fix this without going back to the old SimpleDateFormat, or use an explicitly set timezone because my code needs to run in multiple regions in the world? This timestamp format is obtained by querying another system so I won't be able to change it. It seems SimpleDateFormat can recognize the timezone properly. My test code is below:

public static void main(String[] args) throws ParseException {
    String sTime = "Fri Jun 07 14:07:07 BST 2019";
    DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("EEE MMM dd kk:mm:ss z yyyy");
    ZonedDateTime zd = ZonedDateTime.parse(sTime, FORMATTER);
    System.out.println("The time zone: " + zd.getZone());
    FileTime ftReturn = FileTime.from(zd.toEpochSecond(), TimeUnit.SECONDS);
    System.out.println("File time is: " + ftReturn);

    DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH);
    Date dtDD = df.parse(sTime);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(dtDD);
    FileTime ftReturn1 = FileTime.fromMillis(calendar.getTimeInMillis());
    System.out.println("File time1 is:  " + ftReturn1);
}

Test result:
The time zone: Pacific/Bougainville
File time is: 2019-06-07T03:07:07Z
File time1 is: 2019-06-07T13:07:07Z

sfiss :

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

BST - Asia/Dhaka

So I guess you should not use that abbreviation.

EDIT: Just found this question, which answers it.

So don't use Bangladesh Standard Time ;) Instead use ZoneId.of("Europe/London")

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=142938&siteId=1