/** * Creates a Timestamp instance from the given string. String is in the RFC 3339 format without * the timezone offset (always ends in "Z"). */ public static Timestamp parseTimestamp(String timestamp) { Instant instant = Instant.parse(timestamp); return ofTimeSecondsAndNanos(instant.getEpochSecond(), instant.getNano()); }
@Override public long applyAsLong(Instant value) { return value.getEpochSecond(); } },
@Override public ZoneOffset getStandardOffset(Instant instant) { long epochSec = instant.getEpochSecond(); int index = Arrays.binarySearch(standardTransitions, epochSec); if (index < 0) { // switch negative insert position to start of matched range index = -index - 2; } return standardOffsets[index + 1]; }
@Override public ZoneOffset getStandardOffset(Instant instant) { long epochSec = instant.getEpochSecond(); int index = Arrays.binarySearch(standardTransitions, epochSec); if (index < 0) { // switch negative insert position to start of matched range index = -index - 2; } return standardOffsets[index + 1]; }
/** * Converts an {@code Instant} to a {@code java.sql.Timestamp}. * * @param instant the instant, not null * @return the SQL timestamp, not null */ public static Timestamp toSqlTimestamp(Instant instant) { try { Timestamp ts = new Timestamp(instant.getEpochSecond() * 1000); ts.setNanos(instant.getNano()); return ts; } catch (ArithmeticException ex) { throw new IllegalArgumentException(ex); } }
/** * Converts an {@code Instant} to a {@code java.sql.Timestamp}. * * @param instant the instant, not null * @return the SQL timestamp, not null */ public static Timestamp toSqlTimestamp(Instant instant) { try { Timestamp ts = new Timestamp(instant.getEpochSecond() * 1000); ts.setNanos(instant.getNano()); return ts; } catch (ArithmeticException ex) { throw new IllegalArgumentException(ex); } }
/** * Creates a Timestamp instance from the given string. String is in the RFC 3339 format without * the timezone offset (always ends in "Z"). */ public static Timestamp parseTimestamp(String timestamp) { Instant instant = Instant.parse(timestamp); return ofTimeSecondsAndNanos(instant.getEpochSecond(), instant.getNano()); }
/** * Obtains an instance of {@code ZonedDateTime} from an {@code Instant}. * <p> * This creates a zoned date-time with the same instant as that specified. * Calling {@link #toInstant()} will return an instant equal to the one used here. * <p> * Converting an instant to a zoned date-time is simple as there is only one valid * offset for each instant. * * @param instant the instant to create the date-time from, not null * @param zone the time-zone, not null * @return the zoned date-time, not null * @throws DateTimeException if the result exceeds the supported range */ public static ZonedDateTime ofInstant(Instant instant, ZoneId zone) { Jdk8Methods.requireNonNull(instant, "instant"); Jdk8Methods.requireNonNull(zone, "zone"); return create(instant.getEpochSecond(), instant.getNano(), zone); }
/** * Obtains an instance of {@code ZonedDateTime} from an {@code Instant}. * <p> * This creates a zoned date-time with the same instant as that specified. * Calling {@link #toInstant()} will return an instant equal to the one used here. * <p> * Converting an instant to a zoned date-time is simple as there is only one valid * offset for each instant. * * @param instant the instant to create the date-time from, not null * @param zone the time-zone, not null * @return the zoned date-time, not null * @throws DateTimeException if the result exceeds the supported range */ public static ZonedDateTime ofInstant(Instant instant, ZoneId zone) { Jdk8Methods.requireNonNull(instant, "instant"); Jdk8Methods.requireNonNull(zone, "zone"); return create(instant.getEpochSecond(), instant.getNano(), zone); }
@Override public BsonValue visit(DateTimeValue value, Void arg) { Instant instant = value.getValue().toInstant(ZoneOffset.UTC); return new BsonTimestamp( UnsignedInteger.valueOf(instant.getEpochSecond()).intValue(), instant.getNano() ); }
/** * Obtains an instance of {@code LocalDateTime} from an {@code Instant} and zone ID. * <p> * This creates a local date-time based on the specified instant. * First, the offset from UTC/Greenwich is obtained using the zone ID and instant, * which is simple as there is only one valid offset for each instant. * Then, the instant and offset are used to calculate the local date-time. * * @param instant the instant to create the date-time from, not null * @param zone the time-zone, which may be an offset, not null * @return the local date-time, not null * @throws DateTimeException if the result exceeds the supported range */ public static LocalDateTime ofInstant(Instant instant, ZoneId zone) { Jdk8Methods.requireNonNull(instant, "instant"); Jdk8Methods.requireNonNull(zone, "zone"); ZoneRules rules = zone.getRules(); ZoneOffset offset = rules.getOffset(instant); return ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset); }
/** * Obtains the current date-time from the specified clock. * <p> * This will query the specified clock to obtain the current date-time. * Using this method allows the use of an alternate clock for testing. * The alternate clock may be introduced using {@link Clock dependency injection}. * * @param clock the clock to use, not null * @return the current date-time, not null */ public static LocalDateTime now(Clock clock) { Jdk8Methods.requireNonNull(clock, "clock"); final Instant now = clock.instant(); // called once ZoneOffset offset = clock.getZone().getRules().getOffset(now); return ofEpochSecond(now.getEpochSecond(), now.getNano(), offset); }
/** * Obtains the current date-time from the specified clock. * <p> * This will query the specified clock to obtain the current date-time. * Using this method allows the use of an alternate clock for testing. * The alternate clock may be introduced using {@link Clock dependency injection}. * * @param clock the clock to use, not null * @return the current date-time, not null */ public static LocalDateTime now(Clock clock) { Jdk8Methods.requireNonNull(clock, "clock"); final Instant now = clock.instant(); // called once ZoneOffset offset = clock.getZone().getRules().getOffset(now); return ofEpochSecond(now.getEpochSecond(), now.getNano(), offset); }
/** * Obtains an instance from an instant using the specified time-zone. * * @param chrono the chronology, not null * @param instant the instant, not null * @param zone the zone identifier, not null * @return the zoned date-time, not null */ static <R extends ChronoLocalDate> ChronoZonedDateTimeImpl<R> ofInstant(Chronology chrono, Instant instant, ZoneId zone) { ZoneRules rules = zone.getRules(); ZoneOffset offset = rules.getOffset(instant); Jdk8Methods.requireNonNull(offset, "offset"); // protect against bad ZoneRules LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset); @SuppressWarnings("unchecked") ChronoLocalDateTimeImpl<R> cldt = (ChronoLocalDateTimeImpl<R>) chrono.localDateTime(ldt); return new ChronoZonedDateTimeImpl<R>(cldt, offset, zone); }
/** * Obtains an instance from an instant using the specified time-zone. * * @param chrono the chronology, not null * @param instant the instant, not null * @param zone the zone identifier, not null * @return the zoned date-time, not null */ static <R extends ChronoLocalDate> ChronoZonedDateTimeImpl<R> ofInstant(Chronology chrono, Instant instant, ZoneId zone) { ZoneRules rules = zone.getRules(); ZoneOffset offset = rules.getOffset(instant); Jdk8Methods.requireNonNull(offset, "offset"); // protect against bad ZoneRules LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset); @SuppressWarnings("unchecked") ChronoLocalDateTimeImpl<R> cldt = (ChronoLocalDateTimeImpl<R>) chrono.localDateTime(ldt); return new ChronoZonedDateTimeImpl<R>(cldt, offset, zone); }
/** * Obtains an instance of {@code OffsetDateTime} from an {@code Instant} and zone ID. * <p> * This creates an offset date-time with the same instant as that specified. * Finding the offset from UTC/Greenwich is simple as there is only one valid * offset for each instant. * * @param instant the instant to create the date-time from, not null * @param zone the time-zone, which may be an offset, not null * @return the offset date-time, not null * @throws DateTimeException if the result exceeds the supported range */ public static OffsetDateTime ofInstant(Instant instant, ZoneId zone) { Jdk8Methods.requireNonNull(instant, "instant"); Jdk8Methods.requireNonNull(zone, "zone"); ZoneRules rules = zone.getRules(); ZoneOffset offset = rules.getOffset(instant); LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset); return new OffsetDateTime(ldt, offset); }
/** * Obtains an instance of {@code OffsetDateTime} from an {@code Instant} and zone ID. * <p> * This creates an offset date-time with the same instant as that specified. * Finding the offset from UTC/Greenwich is simple as there is only one valid * offset for each instant. * * @param instant the instant to create the date-time from, not null * @param zone the time-zone, which may be an offset, not null * @return the offset date-time, not null * @throws DateTimeException if the result exceeds the supported range */ public static OffsetDateTime ofInstant(Instant instant, ZoneId zone) { Jdk8Methods.requireNonNull(instant, "instant"); Jdk8Methods.requireNonNull(zone, "zone"); ZoneRules rules = zone.getRules(); ZoneOffset offset = rules.getOffset(instant); LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset); return new OffsetDateTime(ldt, offset); }
/** * Obtains the current date from the specified clock. * <p> * This will query the specified clock to obtain the current date - today. * Using this method allows the use of an alternate clock for testing. * The alternate clock may be introduced using {@link Clock dependency injection}. * * @param clock the clock to use, not null * @return the current date, not null */ public static LocalDate now(Clock clock) { Jdk8Methods.requireNonNull(clock, "clock"); final Instant now = clock.instant(); // called once ZoneOffset offset = clock.getZone().getRules().getOffset(now); long epochSec = now.getEpochSecond() + offset.getTotalSeconds(); // overflow caught later long epochDay = Jdk8Methods.floorDiv(epochSec, SECONDS_PER_DAY); return LocalDate.ofEpochDay(epochDay); }
/** * Obtains the current date from the specified clock. * <p> * This will query the specified clock to obtain the current date - today. * Using this method allows the use of an alternate clock for testing. * The alternate clock may be introduced using {@link Clock dependency injection}. * * @param clock the clock to use, not null * @return the current date, not null */ public static LocalDate now(Clock clock) { Jdk8Methods.requireNonNull(clock, "clock"); final Instant now = clock.instant(); // called once ZoneOffset offset = clock.getZone().getRules().getOffset(now); long epochSec = now.getEpochSecond() + offset.getTotalSeconds(); // overflow caught later long epochDay = Jdk8Methods.floorDiv(epochSec, SECONDS_PER_DAY); return LocalDate.ofEpochDay(epochDay); }
/** * Obtains the current time from the specified clock. * <p> * This will query the specified clock to obtain the current time. * Using this method allows the use of an alternate clock for testing. * The alternate clock may be introduced using {@link Clock dependency injection}. * * @param clock the clock to use, not null * @return the current time, not null */ public static LocalTime now(Clock clock) { Jdk8Methods.requireNonNull(clock, "clock"); // inline OffsetTime factory to avoid creating object and InstantProvider checks final Instant now = clock.instant(); // called once ZoneOffset offset = clock.getZone().getRules().getOffset(now); long secsOfDay = now.getEpochSecond() % SECONDS_PER_DAY; secsOfDay = (secsOfDay + offset.getTotalSeconds()) % SECONDS_PER_DAY; if (secsOfDay < 0) { secsOfDay += SECONDS_PER_DAY; } return LocalTime.ofSecondOfDay(secsOfDay, now.getNano()); }