Rory Lester
Rory Lester

Reputation: 2918

Convert GMT to BST Java

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

Answers (1)

assylias
assylias

Reputation: 328737

With Java 8, the easiest way is probably to:

  • parse the date into a LocalDateTime
  • use it to create a ZonedDateTime using GMT time zone
  • change the time zone to BST
  • get the LocalDateTime of that new date

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

Related Questions