What is UTC? which stands for Coordinated Universal Time. The diagram below shows the standard time offsets of different countries. Some places observe daylight saving time (DST) during their respective summer periods. For example,
Los Angeles (i.e LA) UTC – 8.0 hours (without DST) & UTC – 7.0 hours (with DST)
Berlin UTC + 3.0 hours (without DST) & UTC + 2.0 hours (with DST).
Lets take a reference datetime as 2015-05-15 9:00am and represent it with timezone for LA and Berlin. Note that 15th of May is with DST in both LA and Berlin. Here is the basic code with time zones.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | package test; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; public class Converting { public static void main(String[] args) { // 2015-05-15 9:00am LocalDateTime utcDateTime = LocalDateTime.of(2015, 05, 15, 9, 0); ZoneId zoneLA = ZoneId.of("America/Los_Angeles"); ZoneId zoneBerlin = ZoneId.of("Europe/Berlin"); ZonedDateTime _9AmInLA = ZonedDateTime.of(utcDateTime, zoneLA); ZonedDateTime _9AmInBerlin = ZonedDateTime.of(utcDateTime, zoneBerlin); //UTC-7.00 hours: _9AmInLA = 2015-05-15T09:00-07:00[America/Los_Angeles] System.out.println("_9AmInLA = " + _9AmInLA); //UTC+2.00 hours: _9AmInBerlin = 2015-05-15T09:00+02:00[Europe/Berlin] System.out.println("_9AmInBerlin = " + _9AmInBerlin); //if 9am in LA then UTC (9 am + 7 hours) = 2015-05-15T16:00:00Z System.out.println("if 9am in LA then UTC (9 am + 7 hours) = " + _9AmInLA.toInstant()); //if 9am in Berlin then UTC (9 am - 2 hours) = 2015-05-15T07:00:00Z System.out.println("if 9am in Berlin then UTC (9 am - 2 hours) = " + _9AmInBerlin.toInstant()); boolean laDaylightSavings = zoneLA.getRules().isDaylightSavings(_9AmInLA.toInstant()); boolean berlinDaylightSavings = zoneBerlin.getRules().isDaylightSavings(_9AmInBerlin.toInstant()); System.out.println("laDaylightSavings=" + laDaylightSavings); //true System.out.println("berlinDaylightSavings=" + berlinDaylightSavings); //true } } |
Output:
_9AmInLA = 2015-05-15T09:00-07:00[America/Los_Angeles]
_9AmInBerlin = 2015-05-15T09:00+02:00[Europe/Berlin]
if 9am in LA then UTC (9 am + 7 hours) = 2015-05-15T16:00:00Z
if 9am in Berlin then UTC (9 am – 2 hours) = 2015-05-15T07:00:00Z
laDaylightSavings=true
berlinDaylightSavings=true
Flying from LA to Berlin in 12 Hours
Extending the above example, let us assume that you are flying from LA to Berlin.
Flying Time: 12 Hours
Total Time Difference (with DST): 9 hours (i.e. 7 + 2), derived from LA is UTC-7 and Berlin is UTC+2.
Departure Time in LA: 2015-05-15 9:00
Arrival Time as per LA: 2015-05-15 21:00 (i.e. 2015-05-15 9:00 + 12 HOURS flying time)
Departure Time as per Berlin: 2015-05-15 18:00 (i.e. 2015-05-15 9:00 + 9 HOURS Total Time difference)
Arrival Time in Berlin: 2015-05-16 06:00 (i.e. 2015-05-15 18:00 + 12 HOURS flying time)
In Short, you depart LA at LA time 2015-05-15 9:00 and arrive in Berlin at Berlin time 2015-05-16 06:00 , which is 6.00 am next day. A total of 21 hours (i.e. 12 HOURS flying time + 9 HOURS zone offset).
Here is the Java 8 code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package test; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; public class ZoneBasics { public static void main(String[] args) { // 2015-05-15 9:00am LocalDateTime utcDateTime = LocalDateTime.of(2015, 05, 15, 9, 0); ZoneId zoneLA = ZoneId.of("America/Los_Angeles"); ZoneId zoneBerlin = ZoneId.of("Europe/Berlin"); ZonedDateTime _9AmInLA = ZonedDateTime.of(utcDateTime, zoneLA); //LA to berlin flying time 12 hours. ZonedDateTime arrivalTimeAsPerLA = _9AmInLA.plusHours(12); //arrivalTimeAsPerLA = 2015-05-15T21:00-07:00[America/Los_Angeles] System.out.println("arrivalTimeAsPerLA = " + arrivalTimeAsPerLA); ZonedDateTime arrivalTimeAtBerlin = arrivalTimeAsPerLA.withZoneSameInstant(zoneBerlin); //9 HOURS time difference between LA and Berlin //arrivalTimeAtBerlin = 2015-05-16T06:00+02:00[Europe/Berlin] System.out.println("arrivalTimeAtBerlin = " + arrivalTimeAtBerlin); } } |
Output:
arrivalTimeAsPerLA = 2015-05-15T21:00-07:00[America/Los_Angeles] arrivalTimeAtBerlin = 2015-05-16T06:00+02:00[Europe/Berlin]