-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZoneDateTimeExample.java
More file actions
32 lines (29 loc) · 2.13 KB
/
ZoneDateTimeExample.java
File metadata and controls
32 lines (29 loc) · 2.13 KB
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
package com.learnJava8.dates;
import java.time.*;
public class ZoneDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("ZonedDateTime.now() :: " + zonedDateTime);
System.out.println("zonedDateTime.getOffset() :: " + zonedDateTime.getOffset());
System.out.println("zonedDateTime.getZone() :: " + zonedDateTime.getZone());
ZoneId.getAvailableZoneIds().forEach(System.out::println);
System.out.println("ZoneId.getAvailableZoneIds().size() :: " + ZoneId.getAvailableZoneIds().size());
// get time zones
System.out.println("Europe/London :: " + ZonedDateTime.now(ZoneId.of("Europe/London")));
System.out.println("ZonedDateTime.now(Clock.system(ZoneId.of(\"Europe/London\"))) :: " + ZonedDateTime.now(Clock.system(ZoneId.of("Europe/London"))));
System.out.println("No zone info when using LocalDateTime ::");
System.out.println("LocalDateTime.now(ZoneId.of(\"America/Detroit\")) :: " + LocalDateTime.now(ZoneId.of("America/Detroit")));
System.out.println("LocalDateTime.now(Clock.system(ZoneId.of(\"America/Detroit\")) :: " + LocalDateTime.now(Clock.system(ZoneId.of("America/Detroit"))));
System.out.println("LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()) :: " + LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()));
/**
* convert from localDateTime, instant to ZonedLocalDate and time
*/
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("LocalDateTime.now() :: " + localDateTime);
localDateTime.atZone(ZoneId.of("America/Chicago"));
System.out.println("localDateTime.atZone(ZoneId.of(\"America/Chicago\")) :: " + localDateTime.atZone(ZoneId.of("America/Chicago")));
ZonedDateTime instantNow = Instant.now().atZone(ZoneId.of("America/Detroit"));
System.out.println("Instant.now().atZone(ZoneId.of(\"America/Detroit\")) :: " + instantNow);
System.out.println("localDateTime.atOffset(ZoneOffset.ofHours(6)) :: " + localDateTime.atOffset(ZoneOffset.ofHours(-5)));
}
}