private void copyDateTime( String key, CalendarEventModel.CalendarEventTime dateTime, Map<String, Object> graphCalendar) { Map<String, String> graphDateTime = new HashMap<>(); graphDateTime.put( "dateTime", dateTime.getDateTime().atZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime().toString()); graphDateTime.put("timeZone", "UTC"); graphCalendar.put(key, graphDateTime); }
try { final OffsetDateTime odt = OffsetDateTime.parse(str); return GregorianCalendar.from(odt.atZoneSameInstant(ZoneId.systemDefault())); } catch (final DateTimeParseException e) { return GregorianCalendar.from(odt.atZoneSameInstant(ZoneId.systemDefault())); } catch (final DateTimeParseException e) { return GregorianCalendar.from(odt.atZoneSameInstant(ZoneId.systemDefault())); } catch (final DateTimeParseException e) { return GregorianCalendar.from(odt.atZoneSameInstant(ZoneId.systemDefault())); } catch (final DateTimeParseException e) { return GregorianCalendar.from(ot.atDate(LocalDate.ofEpochDay(0)).atZoneSameInstant(ZoneId.systemDefault())); } catch (final DateTimeParseException e) { return GregorianCalendar.from(ot.atDate(LocalDate.ofEpochDay(0)).atZoneSameInstant(ZoneId.systemDefault())); } catch (final DateTimeParseException e) { return GregorianCalendar.from(ot.atDate(LocalDate.ofEpochDay(0)).atZoneSameInstant(ZoneId.systemDefault())); } catch (final DateTimeParseException e) { return GregorianCalendar.from(ot.atDate(LocalDate.ofEpochDay(0)).atZoneSameInstant(ZoneId.systemDefault())); } catch (final DateTimeParseException e) { final MonthDay md = MonthDay.from(ta); final OffsetTime ot = OffsetTime.from(ta); return GregorianCalendar.from(ot.atDate(y.atMonthDay(md)).atZoneSameInstant(ZoneId.systemDefault()));
OffsetDateTime createdOn = requestDto.getCreatedOn(); OffsetDateTime utc = createdOn.atZoneSameInstant(ZoneId.of("UTC"));
private ZonedDateTime getCutoffDateTime(String datetime){ logger.info("DateTimeFlipCondition: parsing {}", datetime); try{ return OffsetDateTime.parse(datetime).atZoneSameInstant(DateTimeUtils.UTC); } catch (DateTimeParseException e){ logger.error("Could not parse " + datetime + ", expected format yyyy-MM-ddTHH:mm:ssZ"); throw e; } } }
private ZonedDateTime getCutoffDateTime(String datetime){ logger.info("DateTimeFlipCondition: parsing {}", datetime); try{ return OffsetDateTime.parse(datetime).atZoneSameInstant(DateTimeUtils.UTC); } catch (DateTimeParseException e){ logger.error("Could not parse " + datetime + ", expected format yyyy-MM-ddTHH:mm:ssZ"); throw e; } } }
@Override public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException { OffsetDateTime dateTime = rs.getObject(names[TIMESTAMP_UTC_INDEX], OffsetDateTime.class); String zoneIdName = rs.getString(names[ZONE_ID_INDEX]); if (dateTime == null || zoneIdName == null) { return null; } ZoneId zoneId; try { zoneId = ZoneId.of(zoneIdName); } catch (DateTimeException e) { throw new HibernateException("invalid zone id: " + zoneIdName, e); } return dateTime.atZoneSameInstant(zoneId); }
@Override public Instant convert(String value) { TemporalAccessor dt = formatter.parseBest(value.trim(), ZonedDateTime::from, LocalDateTime::from, OffsetDateTime::from, Instant::from, LocalDate::from); if (dt instanceof ZonedDateTime) { return ((ZonedDateTime)dt).toInstant(); } else if (dt instanceof LocalDateTime) { return ((LocalDateTime) dt).atZone(timeZone).toInstant(); } else if (dt instanceof OffsetDateTime) { return ((OffsetDateTime) dt).atZoneSameInstant(timeZone).toInstant(); } else if (dt instanceof Instant) { return ((Instant) dt); } else if (dt instanceof LocalDate) { return ((LocalDate) dt).atStartOfDay(timeZone).toInstant(); } else { return null; } }
private Duration toDuration(OffsetTime first, OffsetTime second) { ZonedDateTime first1 = first.atDate(DateTimeUtil.EPOCH).atZoneSameInstant(DateTimeUtil.UTC); ZonedDateTime second1 = second.atDate(DateTimeUtil.EPOCH).atZoneSameInstant(DateTimeUtil.UTC); long durationInMilliSeconds = getDurationInMilliSeconds(first1, second1); return datatypeFactory.newDuration(durationInMilliSeconds); } }
private Duration toDuration(OffsetTime first, OffsetTime second) { ZonedDateTime first1 = first.atDate(DateTimeUtil.EPOCH).atZoneSameInstant(DateTimeUtil.UTC); ZonedDateTime second1 = second.atDate(DateTimeUtil.EPOCH).atZoneSameInstant(DateTimeUtil.UTC); long durationInMilliSeconds = getDurationInMilliSeconds(first1, second1); return datatypeFactory.newDuration(durationInMilliSeconds); } }
public static ZonedDateTime parse(Clock c, String str) { if (PATTERN_DATETIME.matcher(str).matches()) return LocalDateTime.parse(str).atZone(c.getZone()); if (PATTERN_DATE.matcher(str).matches()) return LocalDate.parse(str).atStartOfDay(c.getZone()); if (PATTERN_MONTH.matcher(str).matches()) return LocalDate.parse(str + "-01").atStartOfDay(c.getZone()); return OffsetDateTime.parse(str).atZoneSameInstant(c.getZone()); }
public static ZonedDateTime fullOffsetFormatToZonedDateTime(String dateStr) throws MeteoException { if (dateStr == null) { return null; } String d = dateStr; // Fix for invalid zone ids in the MET api if (d.charAt(19) == '+') { d = dateStr.substring(0, 22) + ":" + dateStr.substring(22); } try { return OffsetDateTime.parse(d).atZoneSameInstant(ZoneId.of("Z")); } catch (DateTimeParseException e) { throw new MeteoException(e); } } public static ZonedDateTime fullFormatToZonedDateTime(String dateStr) throws MeteoException {
@Override public String format(@Nullable Object value, Locale locale, TimeZone timeZone) { if (timeZone == null || value == null) { return format(value, locale); } OffsetDateTime offsetDateTime = (OffsetDateTime) value; LocalDateTime localDateTime = offsetDateTime.atZoneSameInstant(timeZone.toZoneId()).toLocalDateTime(); return format(localDateTime, locale); }
/** * Gets optionally an event occurrence from the specified data. * @param event an event. * @param occurrenceStartDate a start date. * @return the computed occurrence identifier. */ public static Optional<CalendarEventOccurrence> getBy(CalendarEvent event, Temporal occurrenceStartDate) { Temporal startDate = occurrenceStartDate; if (startDate instanceof OffsetDateTime) { startDate = ((OffsetDateTime) occurrenceStartDate).atZoneSameInstant(ZoneOffset.UTC) .toOffsetDateTime(); } return getById(generateId(event, startDate)); }
@Override public OffsetDateTime fromNonNullValue(Timestamp value) { ZoneId currentDatabaseZone = getDatabaseZone() == null ? getDefaultZoneId() : getDatabaseZone(); ZoneId currentJavaZone = javaZone == null ? getDefaultZoneId() : javaZone; OffsetDateTime dateTime = OffsetDateTime.ofInstant(Instant.ofEpochMilli(value.getTime()), currentDatabaseZone); ZonedDateTime zonedDateTime = dateTime.with(ChronoField.NANO_OF_SECOND, value.getNanos()).atZoneSameInstant(currentJavaZone); dateTime = zonedDateTime.toOffsetDateTime(); return dateTime; }
@Override public OffsetTime fromNonNullValue(Timestamp value) { ZoneId currentDatabaseZone = getDatabaseZone() == null ? getDefaultZoneOffset() : getDatabaseZone(); ZoneId currentJavaZone = javaZone == null ? getDefaultZoneOffset() : javaZone; OffsetDateTime dateTime = OffsetDateTime.ofInstant(Instant.ofEpochMilli(value.getTime()), currentDatabaseZone); dateTime = dateTime.with(ChronoField.NANO_OF_SECOND, value.getNanos()).atZoneSameInstant(currentJavaZone).toOffsetDateTime(); OffsetTime time = dateTime.toOffsetTime(); return time; }
public Calendar fromString(String string) { final OffsetTime parsedOffsetTime = OffsetTime.parse( string, FORMATTER ); return GregorianCalendar.from( parsedOffsetTime.atDate( LocalDate.MIN ).atZoneSameInstant( parsedOffsetTime.getOffset() ) ); }
/** * Triggers this reminder at the specified date time. The timezone of the specified date time * will be set in the timezone of the user behind this reminder. * @param dateTime the date time at which this reminder will be triggered once scheduled. * @return itself. */ public DateTimeReminder triggerAt(final OffsetDateTime dateTime) { final ZoneId userZoneId = User.getById(getUserId()).getUserPreferences().getZoneId(); this.dateTime = dateTime.atZoneSameInstant(userZoneId).toOffsetDateTime(); return this; }
/** * Converts {@link OffsetDateTime} to {@link TIMESTAMPTZ}. * * @param attribute the value to be converted, possibly {@code null} * @return the converted data, possibly {@code null} */ public static TIMESTAMPTZ offsetDateTimeToTimestamptz(OffsetDateTime attribute) { if (attribute == null) { return null; } byte[] bytes = newTimestamptzBuffer(); ZonedDateTime utc = attribute.atZoneSameInstant(UTC); writeDateTime(bytes, utc.toLocalDateTime()); ZoneOffset offset = attribute.getOffset(); writeZoneOffset(bytes, offset); return new TIMESTAMPTZ(bytes); }
/** * Converts {@link OffsetDateTime} to {@link TIMESTAMPTZ}. * * @param attribute the value to be converted, possibly {@code null} * @return the converted data, possibly {@code null} */ public static TIMESTAMPTZ offsetDateTimeToTimestamptz(OffsetDateTime attribute) { if (attribute == null) { return null; } byte[] bytes = newTimestamptzBuffer(); ZonedDateTime utc = attribute.atZoneSameInstant(UTC); writeDateTime(bytes, utc.toLocalDateTime()); ZoneOffset offset = attribute.getOffset(); writeZoneOffset(bytes, offset); return new TIMESTAMPTZ(bytes); }
@Test public void testObjectToOffsetDateTime() throws Exception { ZoneId zoneId = ZoneId.systemDefault(); OffsetDateTime offsetDateTime = OffsetDateTime.now(zoneId); testObjectToOffsetDateTime(null, null); testObjectToOffsetDateTime(offsetDateTime, offsetDateTime); testObjectToOffsetDateTime(offsetDateTime.toLocalDateTime(), offsetDateTime); testObjectToOffsetDateTime(offsetDateTime.toInstant(), offsetDateTime); testObjectToOffsetDateTime(offsetDateTime.atZoneSameInstant(zoneId), offsetDateTime); testObjectToOffsetDateTime(offsetDateTime.atZoneSameInstant(zoneId), offsetDateTime); testObjectToOffsetDateTime(offsetDateTime.toLocalDate(), offsetDateTime.truncatedTo(ChronoUnit.DAYS)); testObjectToOffsetDateTime(Date.from(offsetDateTime.toInstant()), offsetDateTime.truncatedTo(ChronoUnit.MILLIS)); try { testObjectToOffsetDateTime("a string", offsetDateTime); fail(); } catch (IllegalArgumentException e) { // expected } }