Use a ZoneId
and a ZonedDateTime
.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss", Locale.ENGLISH);
ZoneId zone = ZoneId.of("Europe/Berlin");
long seconds = 1_556_000_000;
ZonedDateTime dateTime = Instant.ofEpochSecond(seconds).atZone(zone);
String formattedDateTime = dateTime.format(formatter);
System.out.println(formattedDateTime);
The output from this snippet is:
23.04.2019 08:13:20
My example seconds value corresponds to 06:13:20 UTC, so you’ve got your two hours offset from UTC respected.
Summer time (DST) is built in, so in winter you will get only 1 hour from UTC as you should. Historic changes in the offset work out of the box too, and known future changes (what happens in Germany after 2021 no one knows).