@Override public ZonedDateTime convert(OffsetDateTime source) { return source.toZonedDateTime(); } }
@Override public ZonedDateTime convert(OffsetDateTime source) { return source.toZonedDateTime(); } }
public static DateTimeValue datetime( OffsetDateTime datetime ) { return new DateTimeValue( requireNonNull( datetime, "OffsetDateTime" ).toZonedDateTime() ); }
@Override public ZonedDateTime convert(String source) { ZonedDateTime dateTime; try { long epoch = Long.parseLong(source); dateTime = Instant.ofEpochMilli(epoch).atOffset(ZoneOffset.ofTotalSeconds(0)) .toZonedDateTime(); } catch (NumberFormatException e) { // try ZonedDateTime instead dateTime = ZonedDateTime.parse(source); } return dateTime; } }
@Override @SuppressWarnings("unchecked") public <X> X unwrap(OffsetTime offsetTime, Class<X> type, WrapperOptions options) { if ( offsetTime == null ) { return null; } if ( OffsetTime.class.isAssignableFrom( type ) ) { return (X) offsetTime; } if ( java.sql.Time.class.isAssignableFrom( type ) ) { return (X) java.sql.Time.valueOf( offsetTime.toLocalTime() ); } final ZonedDateTime zonedDateTime = offsetTime.atDate( LocalDate.of( 1970, 1, 1 ) ).toZonedDateTime(); if ( Timestamp.class.isAssignableFrom( type ) ) { return (X) Timestamp.valueOf( zonedDateTime.toLocalDateTime() ); } if ( Calendar.class.isAssignableFrom( type ) ) { return (X) GregorianCalendar.from( zonedDateTime ); } final Instant instant = zonedDateTime.toInstant(); if ( Long.class.isAssignableFrom( type ) ) { return (X) Long.valueOf( instant.toEpochMilli() ); } if ( java.util.Date.class.isAssignableFrom( type ) ) { return (X) java.util.Date.from( instant ); } throw unknownUnwrap( type ); }
@Override @SuppressWarnings("unchecked") public <X> X unwrap(OffsetDateTime offsetDateTime, Class<X> type, WrapperOptions options) { if ( offsetDateTime == null ) { return null; } if ( OffsetDateTime.class.isAssignableFrom( type ) ) { return (X) offsetDateTime; } if ( Calendar.class.isAssignableFrom( type ) ) { return (X) GregorianCalendar.from( offsetDateTime.toZonedDateTime() ); } if ( Timestamp.class.isAssignableFrom( type ) ) { return (X) Timestamp.from( offsetDateTime.toInstant() ); } if ( java.sql.Date.class.isAssignableFrom( type ) ) { return (X) java.sql.Date.from( offsetDateTime.toInstant() ); } if ( java.sql.Time.class.isAssignableFrom( type ) ) { return (X) java.sql.Time.from( offsetDateTime.toInstant() ); } if ( Date.class.isAssignableFrom( type ) ) { return (X) Date.from( offsetDateTime.toInstant() ); } if ( Long.class.isAssignableFrom( type ) ) { return (X) Long.valueOf( offsetDateTime.toInstant().toEpochMilli() ); } throw unknownUnwrap( type ); }
@Override public ZonedDateTime convert(OffsetDateTime source) { return source.toZonedDateTime(); } }
static final DateTimeFormatter DATE_TIME_OPTIONAL_OFFSET = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss[xxx]"); OffsetDateTime odt = DATE_TIME_OPTIONAL_OFFSET.withZone(ZoneOffset.UTC).parse(input, OffsetDateTime::from); ZonedDateTime zdt = odt.toZonedDateTime(); // containing a fixed offset
/** * Returns a generally equivalent instance of {@link java.util.Calendar}. * The time value of the returned calendar is truncated to milliseconds and the time zone * is based on the offset of this date/time. * * @param self an OffsetDateTime * @return a java.util.Calendar * @since 2.5.0 */ public static Calendar toCalendar(final OffsetDateTime self) { return toCalendar(self.toZonedDateTime()); }
public static Value value( OffsetDateTime offsetDateTime ) { return new DateTimeValue( offsetDateTime.toZonedDateTime() ); }
public static ZonedDateTime toZonedDateTimeUtc(@Nonnull Timestamp timestamp) { checkNotNull(timestamp, "timestamp"); return toOffsetDateTimeUtc(timestamp).toZonedDateTime(); }
public static ZonedDateTime toZonedDateTimeUtc(@Nonnull Timestamp timestamp) { checkNotNull(timestamp, "timestamp"); return toOffsetDateTimeUtc(timestamp).toZonedDateTime(); }
public static DateTimeValue datetime( OffsetDateTime datetime ) { return new DateTimeValue( requireNonNull( datetime, "OffsetDateTime" ).toZonedDateTime() ); }
private Date parseDate(String stringHolder) { return dateCache.computeIfAbsent(stringHolder, (k) -> { final OffsetDateTime dateTime = OffsetDateTime.parse(stringHolder); return GregorianCalendar.from(dateTime.toZonedDateTime()).getTime(); }); }
@Override public ZonedDateTime time(ZonedDateTime from) { if (from == null) { return null; } OffsetTime offsetTime = from.toOffsetDateTime().toOffsetTime(); return offsetTime.atDate(LocalDate.MIN).toZonedDateTime(); }
@Override public ZonedDateTime getActivationDateForActiveScript(User user) throws StorageException, ScriptNotFoundException { Optional<JPASieveScript> script = findActiveSieveScript(user); JPASieveScript activeSieveScript = script.orElseThrow(() -> new ScriptNotFoundException("Unable to find active script for user " + user.asString())); return activeSieveScript.getActivationDateTime().toZonedDateTime(); }
@Override public ZonedDateTime time(ZonedDateTime from) { if (from == null) { return null; } OffsetTime offsetTime = from.toOffsetDateTime().toOffsetTime(); return offsetTime.atDate(LocalDate.MIN).toZonedDateTime(); }
@Override protected Object decodeValue(Context context, Type type, Short typeLength, Integer typeModifier, CharSequence buffer, Class<?> targetClass, Object targetContext) throws IOException { Calendar calendar = targetContext != null ? (Calendar) targetContext : Calendar.getInstance(); if (buffer.equals(POS_INFINITY) || buffer.equals(NEG_INFINITY)) { return convertInfinityOutput(buffer.equals(POS_INFINITY), type, targetClass); } TemporalAccessor parsed = context.getTimestampFormat().getParser().parse(buffer); ZonedDateTime dateTime; if (parsed.isSupported(ChronoField.OFFSET_SECONDS)) { dateTime = OffsetDateTime.from(parsed).toZonedDateTime().withZoneSameInstant(UTC_ID); } else { dateTime = LocalDateTime.from(parsed).atZone(calendar.getTimeZone().toZoneId()); } return convertOutput(context, type, dateTime, targetClass, calendar); }
@Override public void setNonNullParameter(PreparedStatement ps, int i, OffsetDateTime parameter, JdbcType jdbcType) throws SQLException { // Postgres do not work with offsets > than 15 hours, maybe other DBs too int offset = parameter.get(ChronoField.OFFSET_SECONDS); if (Math.abs(offset) > 54000) { parameter = parameter.withOffsetSameInstant(ZoneOffset.ofHours(offset > 0 ? 15 : -15)); } ps.setTimestamp( i, Timestamp.from(parameter.toInstant()), GregorianCalendar.from(parameter.toZonedDateTime()) ); }
private ZonedDateTime makeDateTime(String literal) { if (StringUtils.isBlank(literal)) { return null; } try { literal = DateTimeUtil.fixDateTimeFormat(literal); if (DateTimeUtil.hasZone(literal)) { return makeZonedDateTime(literal); } else if (DateTimeUtil.hasOffset(literal)) { return OffsetDateTime.parse(literal).toZonedDateTime(); } else { return makeZonedDateTime(literal + "Z"); } } catch (Throwable e) { String message = String.format("makeDateTime(%s)", literal); logError(message, e); return null; } }