Back
Close

Time Difference Using Java 8

Harinath
2,126 views

Time Difference Using Java 8!

// {...}
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

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants