@Override public ZonedDateTime queryFrom(TemporalAccessor temporal) { return ZonedDateTime.from(temporal); } };
/** * Resolves the new local date-time using the offset to identify the instant. * * @param newDateTime the new local date-time, not null * @return the zoned date-time, not null */ private ZonedDateTime resolveInstant(LocalDateTime newDateTime) { return ofInstant(newDateTime, offset, zone); }
@Override public ZonedDateTime apply(ZonedDateTime zonedDateTime, ZoneId zoneId) { return zonedDateTime.withZoneSameInstant(zoneId); } }
@Override public long applyAsLong(ZonedDateTime dt) { return dt.toInstant().toEpochMilli(); } },
ZonedDateTime end = ZonedDateTime.from(endExclusive); if (unit instanceof ChronoUnit) { end = end.withZoneSameInstant(zone); if (unit.isDateBased()) { return dateTime.until(end.dateTime, unit); } else { return toOffsetDateTime().until(end.toOffsetDateTime(), unit);
/** * Converts a {@code ZonedDateTime} to a {@code Calendar}. * <p> * The resulting {@code GregorianCalendar} is pure Gregorian and uses * ISO week definitions, starting on Monday and with 4 days in a minimal week. * <p> * Fractions of the instant smaller than milliseconds will be dropped. * * @param zdt the zoned date-time, not null * @return the calendar, not null * @throws IllegalArgumentException if the conversion fails */ public static GregorianCalendar toGregorianCalendar(ZonedDateTime zdt) { TimeZone zone = toTimeZone(zdt.getZone()); GregorianCalendar cal = new GregorianCalendar(zone); cal.setGregorianChange(new Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); try { cal.setTimeInMillis(zdt.toInstant().toEpochMilli()); } catch (ArithmeticException ex) { throw new IllegalArgumentException(ex); } return cal; }
ChronoField f = (ChronoField) field; switch (f) { case INSTANT_SECONDS: return create(newValue, getNano(), zone); case OFFSET_SECONDS: { ZoneOffset offset = ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue)); return resolveOffset(offset); return resolveLocal(dateTime.with(field, newValue));
/** * Converts this date-time to a {@code ZonedDateTime} using the offset as the zone ID. * <p> * This creates the simplest possible {@code ZonedDateTime} using the offset * as the zone ID. * <p> * To control the time-zone used, see {@link #atZoneSameInstant(ZoneId)} and * {@link #atZoneSimilarLocal(ZoneId)}. * * @return a zoned date-time representing the same local date-time and offset, not null */ public ZonedDateTime toZonedDateTime() { return ZonedDateTime.of(dateTime, offset); }
if (field instanceof ChronoField) { switch ((ChronoField) field) { case INSTANT_SECONDS: return toEpochSecond(); case OFFSET_SECONDS: return getOffset().getTotalSeconds();
/** * Obtains an instance of {@code ZonedDateTime} from a text string such as * {@code 2007-12-23T10:15:30+01:00[Europe/Paris]}. * <p> * The string must represent a valid date-time and is parsed using * {@link org.threeten.bp.format.DateTimeFormatter#ISO_ZONED_DATE_TIME}. * * @param text the text to parse such as "2007-12-23T10:15:30+01:00[Europe/Paris]", not null * @return the parsed zoned date-time, not null * @throws DateTimeParseException if the text cannot be parsed */ public static ZonedDateTime parse(CharSequence text) { return parse(text, DateTimeFormatter.ISO_ZONED_DATE_TIME); }
public LocalDate deserialize(JsonParser jsonparser, DeserializationContext context) throws IOException, JsonProcessingException { String date = jsonparser.getText(); LocalDate formattedDate; Pattern datePatt = Pattern.compile("^/Date\\((\\d+)([+-]\\d+)?\\)/$"); Matcher m = datePatt.matcher(date); if (m.matches()) { Long l = Long.parseLong(m.group(1)); formattedDate = Instant.ofEpochMilli(l).atZone(ZoneId.systemDefault()).toLocalDate(); } else { throw new IllegalArgumentException("Wrong date format"); } return formattedDate; } }
@Override public long applyAsLong(ZonedDateTime dt) { return dt.toEpochSecond(); } },
@Override public int applyAsInt(ZonedDateTime dt) { return dt.getNano(); } },
private String convertDateToSlackTimestamp(ZonedDateTime date) { return (date.toInstant().toEpochMilli() / 1000) + ".123456"; }
ZonedDateTime end = ZonedDateTime.from(endExclusive); if (unit instanceof ChronoUnit) { end = end.withZoneSameInstant(zone); if (unit.isDateBased()) { return dateTime.until(end.dateTime, unit); } else { return toOffsetDateTime().until(end.toOffsetDateTime(), unit);
/** * Converts a {@code ZonedDateTime} to a {@code Calendar}. * <p> * The resulting {@code GregorianCalendar} is pure Gregorian and uses * ISO week definitions, starting on Monday and with 4 days in a minimal week. * <p> * Fractions of the instant smaller than milliseconds will be dropped. * * @param zdt the zoned date-time, not null * @return the calendar, not null * @throws IllegalArgumentException if the conversion fails */ public static GregorianCalendar toGregorianCalendar(ZonedDateTime zdt) { TimeZone zone = toTimeZone(zdt.getZone()); GregorianCalendar cal = new GregorianCalendar(zone); cal.setGregorianChange(new Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); try { cal.setTimeInMillis(zdt.toInstant().toEpochMilli()); } catch (ArithmeticException ex) { throw new IllegalArgumentException(ex); } return cal; }
ChronoField f = (ChronoField) field; switch (f) { case INSTANT_SECONDS: return create(newValue, getNano(), zone); case OFFSET_SECONDS: { ZoneOffset offset = ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue)); return resolveOffset(offset); return resolveLocal(dateTime.with(field, newValue));
/** * Converts this date-time to a {@code ZonedDateTime} using the offset as the zone ID. * <p> * This creates the simplest possible {@code ZonedDateTime} using the offset * as the zone ID. * <p> * To control the time-zone used, see {@link #atZoneSameInstant(ZoneId)} and * {@link #atZoneSimilarLocal(ZoneId)}. * * @return a zoned date-time representing the same local date-time and offset, not null */ public ZonedDateTime toZonedDateTime() { return ZonedDateTime.of(dateTime, offset); }
if (field instanceof ChronoField) { switch ((ChronoField) field) { case INSTANT_SECONDS: return toEpochSecond(); case OFFSET_SECONDS: return getOffset().getTotalSeconds();