Returns the current ZonedDatetime from the system clock in the default time-zone.
Returns the current ZonedDatetime from the system clock and in provided ZoneId.
Returns the current ZonedDatetime from provided clock.
package com.logicbig.example.zoneddatetime;
import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class NowExample {
public static void main(String... args) {
ZonedDateTime d = ZonedDateTime.now();
System.out.println(d);
ZonedDateTime d2 = ZonedDateTime.now(ZoneId.of("America/New_York"));
System.out.println(d2);
ZonedDateTime d3 = ZonedDateTime.now(Clock.tickSeconds(ZoneId.of("Asia/Seoul")));
System.out.println(d3);
}
}
Output
2017-05-01T16:01:35.193-05:00[America/Chicago]
2017-05-01T17:01:35.232-04:00[America/New_York]
2017-05-02T06:01:35+09:00[Asia/Seoul]
This example shows the difference between using ZoneId.of(..) and Clock methods.
package com.logicbig.example.zoneddatetime;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class NowExample2 {
public static void main(String... args) {
ZoneId zoneId = ZoneId.of("America/New_York");
ZonedDateTime d2 = ZonedDateTime.now(zoneId);
System.out.println(d2);
ZonedDateTime d3 = ZonedDateTime.now(Clock.tickSeconds(zoneId));
System.out.println(d3);
ZonedDateTime d4 = ZonedDateTime.now(Clock.tickMinutes(zoneId));
System.out.println(d4);
Clock clock = Clock.fixed(Instant.now(), zoneId);
ZonedDateTime d5 = ZonedDateTime.now(clock);
System.out.println(d5);
}
}
Output
2017-05-01T17:01:37.277-04:00[America/New_York]
2017-05-01T17:01:37-04:00[America/New_York]
2017-05-01T17:01-04:00[America/New_York]
2017-05-01T17:01:37.322-04:00[America/New_York]