06: Understanding TimeZones with Java 8 examples

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.

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
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:

Output:

arrivalTimeAsPerLA = 2015-05-15T21:00-07:00[America/Los_Angeles] arrivalTimeAtBerlin = 2015-05-16T06:00+02:00[Europe/Berlin]

Top