Reputation: 2918
I have a date format yyyy/mm/dd hh:mm:ss
in GMT
format. I want to convert the date from GMT
format to BST
time. What's the simplest way to do this?
Upvotes: 1
Views: 5473
Reputation: 328737
With Java 8, the easiest way is probably to:
Sample example:
String date = "2015/03/09 10:32:00";
LocalDateTime gmt = LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));
ZonedDateTime instant = ZonedDateTime.of(gmt, ZoneId.of("GMT"));
LocalDateTime bst = instant.withZoneSameInstant(ZoneId.of("Europe/London")).toLocalDateTime();
System.out.println(bst);
If you change the month to July, for example, you should see an offset of one hour as expected.
(I assumed that BST is British Summer Time - it could also be Bangladesh Standard Time: in general it is better to use the full name of the time zone to avoid ambiguities).
Upvotes: 3