Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Time Difference Using Java 8!
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
// {
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
// }
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());
//{
}
}
//}
Press desired key combination and then press ENTER.
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