Created
July 19, 2018 08:23
-
-
Save perforb/9be548d5a5443f58f8bb321b82cd6b6d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.todo.library.datetime; | |
import java.time.Clock; | |
import java.time.Instant; | |
import java.time.ZoneId; | |
import java.time.ZoneOffset; | |
import java.time.ZonedDateTime; | |
import java.time.temporal.ChronoField; | |
import java.util.Objects; | |
public class DateTimeProvider { | |
private final Clock clock; | |
public DateTimeProvider(Clock clock) { | |
Objects.requireNonNull(clock); | |
this.clock = clock; | |
} | |
public Instant instant() { | |
return clock.instant(); | |
} | |
public ZonedDateTime now() { | |
return ZonedDateTime.now(clock.getZone()); | |
} | |
public ZonedDateTime now(ZoneId zoneId) { | |
return ZonedDateTime.ofInstant(clock.instant(), zoneId); | |
} | |
public static void main(String[] args) { | |
DateTimeProvider provider = new DateTimeProvider(Clock.system(ZoneId.of("Asia/Tokyo"))); | |
ZonedDateTime now = provider.now(); | |
ZoneOffset offset = now.getOffset(); | |
System.out.println(now); | |
System.out.println(provider.now(ZoneId.of("UTC"))); | |
System.out.println(offset); | |
System.out.println(offset.getId()); | |
System.out.println(offset.normalized()); | |
System.out.println(offset.range(ChronoField.OFFSET_SECONDS)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2018-07-19T17:21:46.667+09:00[Asia/Tokyo] | |
2018-07-19T08:21:46.667Z[UTC] | |
+09:00 | |
+09:00 | |
+09:00 | |
-64800 - 64800 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment