Skip to content

Instantly share code, notes, and snippets.

@perforb
Created July 19, 2018 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perforb/9be548d5a5443f58f8bb321b82cd6b6d to your computer and use it in GitHub Desktop.
Save perforb/9be548d5a5443f58f8bb321b82cd6b6d to your computer and use it in GitHub Desktop.
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));
}
}
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