I am in the process of migrating my code to use the java.time
package but I found that 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.
Does anybody know how I can fix this without going back to the old SimpleDateFormat, or use an explicit timezone? 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:
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
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")