Time Difference Using Java 8

Harinath
1,325 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Time Difference Using Java 8!

1
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// {...}
ZoneId singaporeZone = ZoneId.of("Asia/Singapore");
ZonedDateTime dateTimeInSingapore =
ZonedDateTime.of(LocalDateTime.of(2016,Month.JANUARY,1,6,0), singaporeZone);
ZoneId aucklandZone = ZoneId.of("Pacific/Auckland");
ZonedDateTime dateTimeinAuckland =
dateTimeInSingapore.withZoneSameInstant(aucklandZone);
Duration timeDifference = Duration.between(
dateTimeInSingapore.toLocalTime(), dateTimeinAuckland.toLocalTime());
System.out.printf("Time difference between %s and %s zone is %d hours", singaporeZone,aucklandZone,timeDifference.toHours());
//{...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Advanced usage

If you want a more complex example (external libraries, viewers...), use the Advanced Java template

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content