java.time.OffsetDateTime.atZoneSameInstant()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(11.5k)|赞(0)|评价(0)|浏览(137)

本文整理了Java中java.time.OffsetDateTime.atZoneSameInstant()方法的一些代码示例,展示了OffsetDateTime.atZoneSameInstant()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。OffsetDateTime.atZoneSameInstant()方法的具体详情如下:
包路径:java.time.OffsetDateTime
类名称:OffsetDateTime
方法名:atZoneSameInstant

OffsetDateTime.atZoneSameInstant介绍

[英]Combines this date-time with a time-zone to create a ZonedDateTimeensuring that the result has the same instant.

This returns a ZonedDateTime formed from this date-time and the specified time-zone. This conversion will ignore the visible local date-time and use the underlying instant instead. This avoids any problems with local time-line gaps or overlaps. The result might have different values for fields such as hour, minute an even day.

To attempt to retain the values of the fields, use #atZoneSimilarLocal(ZoneId). To use the offset as the zone ID, use #toZonedDateTime().
[中]将此日期时间与时区组合,以创建ZonedDateTime,确保结果具有相同的瞬间。
这将返回从该日期时间和指定时区形成的ZonedDateTime。此转换将忽略可见的本地日期时间,而是使用基础即时。这避免了任何与当地时间线间隔或重叠有关的问题。结果可能会有不同的字段值,例如小时、分钟甚至一天。
要尝试保留字段的值,请使用#atZoneSimilarLocal(ZoneId)。要使用偏移量作为分区ID,请使用#toZonedDateTime()。

代码示例

代码示例来源:origin: google/data-transfer-project

  1. private void copyDateTime(
  2. String key,
  3. CalendarEventModel.CalendarEventTime dateTime,
  4. Map<String, Object> graphCalendar) {
  5. Map<String, String> graphDateTime = new HashMap<>();
  6. graphDateTime.put(
  7. "dateTime",
  8. dateTime.getDateTime().atZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime().toString());
  9. graphDateTime.put("timeZone", "UTC");
  10. graphCalendar.put(key, graphDateTime);
  11. }

代码示例来源:origin: com.thoughtworks.xstream/xstream

  1. try {
  2. final OffsetDateTime odt = OffsetDateTime.parse(str);
  3. return GregorianCalendar.from(odt.atZoneSameInstant(ZoneId.systemDefault()));
  4. } catch (final DateTimeParseException e) {
  5. return GregorianCalendar.from(odt.atZoneSameInstant(ZoneId.systemDefault()));
  6. } catch (final DateTimeParseException e) {
  7. return GregorianCalendar.from(odt.atZoneSameInstant(ZoneId.systemDefault()));
  8. } catch (final DateTimeParseException e) {
  9. return GregorianCalendar.from(odt.atZoneSameInstant(ZoneId.systemDefault()));
  10. } catch (final DateTimeParseException e) {
  11. return GregorianCalendar.from(ot.atDate(LocalDate.ofEpochDay(0)).atZoneSameInstant(ZoneId.systemDefault()));
  12. } catch (final DateTimeParseException e) {
  13. return GregorianCalendar.from(ot.atDate(LocalDate.ofEpochDay(0)).atZoneSameInstant(ZoneId.systemDefault()));
  14. } catch (final DateTimeParseException e) {
  15. return GregorianCalendar.from(ot.atDate(LocalDate.ofEpochDay(0)).atZoneSameInstant(ZoneId.systemDefault()));
  16. } catch (final DateTimeParseException e) {
  17. return GregorianCalendar.from(ot.atDate(LocalDate.ofEpochDay(0)).atZoneSameInstant(ZoneId.systemDefault()));
  18. } catch (final DateTimeParseException e) {
  19. final MonthDay md = MonthDay.from(ta);
  20. final OffsetTime ot = OffsetTime.from(ta);
  21. return GregorianCalendar.from(ot.atDate(y.atMonthDay(md)).atZoneSameInstant(ZoneId.systemDefault()));

代码示例来源:origin: stackoverflow.com

  1. OffsetDateTime createdOn = requestDto.getCreatedOn();
  2. OffsetDateTime utc = createdOn.atZoneSameInstant(ZoneId.of("UTC"));

代码示例来源:origin: com.github.feature-flip/flips-core

  1. private ZonedDateTime getCutoffDateTime(String datetime){
  2. logger.info("DateTimeFlipCondition: parsing {}", datetime);
  3. try{
  4. return OffsetDateTime.parse(datetime).atZoneSameInstant(DateTimeUtils.UTC);
  5. }
  6. catch (DateTimeParseException e){
  7. logger.error("Could not parse " + datetime + ", expected format yyyy-MM-ddTHH:mm:ssZ");
  8. throw e;
  9. }
  10. }
  11. }

代码示例来源:origin: Feature-Flip/flips

  1. private ZonedDateTime getCutoffDateTime(String datetime){
  2. logger.info("DateTimeFlipCondition: parsing {}", datetime);
  3. try{
  4. return OffsetDateTime.parse(datetime).atZoneSameInstant(DateTimeUtils.UTC);
  5. }
  6. catch (DateTimeParseException e){
  7. logger.error("Could not parse " + datetime + ", expected format yyyy-MM-ddTHH:mm:ssZ");
  8. throw e;
  9. }
  10. }
  11. }

代码示例来源:origin: marschall/threeten-jpa

  1. @Override
  2. public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
  3. OffsetDateTime dateTime = rs.getObject(names[TIMESTAMP_UTC_INDEX], OffsetDateTime.class);
  4. String zoneIdName = rs.getString(names[ZONE_ID_INDEX]);
  5. if (dateTime == null || zoneIdName == null) {
  6. return null;
  7. }
  8. ZoneId zoneId;
  9. try {
  10. zoneId = ZoneId.of(zoneIdName);
  11. } catch (DateTimeException e) {
  12. throw new HibernateException("invalid zone id: " + zoneIdName, e);
  13. }
  14. return dateTime.atZoneSameInstant(zoneId);
  15. }

代码示例来源:origin: org.graylog2.repackaged/grok

  1. @Override
  2. public Instant convert(String value) {
  3. TemporalAccessor dt = formatter.parseBest(value.trim(), ZonedDateTime::from, LocalDateTime::from, OffsetDateTime::from, Instant::from, LocalDate::from);
  4. if (dt instanceof ZonedDateTime) {
  5. return ((ZonedDateTime)dt).toInstant();
  6. } else if (dt instanceof LocalDateTime) {
  7. return ((LocalDateTime) dt).atZone(timeZone).toInstant();
  8. } else if (dt instanceof OffsetDateTime) {
  9. return ((OffsetDateTime) dt).atZoneSameInstant(timeZone).toInstant();
  10. } else if (dt instanceof Instant) {
  11. return ((Instant) dt);
  12. } else if (dt instanceof LocalDate) {
  13. return ((LocalDate) dt).atStartOfDay(timeZone).toInstant();
  14. } else {
  15. return null;
  16. }
  17. }

代码示例来源:origin: goldmansachs/jdmn

  1. private Duration toDuration(OffsetTime first, OffsetTime second) {
  2. ZonedDateTime first1 = first.atDate(DateTimeUtil.EPOCH).atZoneSameInstant(DateTimeUtil.UTC);
  3. ZonedDateTime second1 = second.atDate(DateTimeUtil.EPOCH).atZoneSameInstant(DateTimeUtil.UTC);
  4. long durationInMilliSeconds = getDurationInMilliSeconds(first1, second1);
  5. return datatypeFactory.newDuration(durationInMilliSeconds);
  6. }
  7. }

代码示例来源:origin: com.goldmansachs.jdmn/jdmn-core

  1. private Duration toDuration(OffsetTime first, OffsetTime second) {
  2. ZonedDateTime first1 = first.atDate(DateTimeUtil.EPOCH).atZoneSameInstant(DateTimeUtil.UTC);
  3. ZonedDateTime second1 = second.atDate(DateTimeUtil.EPOCH).atZoneSameInstant(DateTimeUtil.UTC);
  4. long durationInMilliSeconds = getDurationInMilliSeconds(first1, second1);
  5. return datatypeFactory.newDuration(durationInMilliSeconds);
  6. }
  7. }

代码示例来源:origin: com.guestful.module/guestful.module.jsr310-extensions

  1. public static ZonedDateTime parse(Clock c, String str) {
  2. if (PATTERN_DATETIME.matcher(str).matches()) return LocalDateTime.parse(str).atZone(c.getZone());
  3. if (PATTERN_DATE.matcher(str).matches()) return LocalDate.parse(str).atStartOfDay(c.getZone());
  4. if (PATTERN_MONTH.matcher(str).matches()) return LocalDate.parse(str + "-01").atStartOfDay(c.getZone());
  5. return OffsetDateTime.parse(str).atZoneSameInstant(c.getZone());
  6. }

代码示例来源:origin: no.api.meteo/meteo-core

  1. public static ZonedDateTime fullOffsetFormatToZonedDateTime(String dateStr) throws MeteoException {
  2. if (dateStr == null) {
  3. return null;
  4. }
  5. String d = dateStr;
  6. // Fix for invalid zone ids in the MET api
  7. if (d.charAt(19) == '+') {
  8. d = dateStr.substring(0, 22) + ":" + dateStr.substring(22);
  9. }
  10. try {
  11. return OffsetDateTime.parse(d).atZoneSameInstant(ZoneId.of("Z"));
  12. } catch (DateTimeParseException e) {
  13. throw new MeteoException(e);
  14. }
  15. }
  16. public static ZonedDateTime fullFormatToZonedDateTime(String dateStr) throws MeteoException {

代码示例来源:origin: com.haulmont.cuba/cuba-global

  1. @Override
  2. public String format(@Nullable Object value, Locale locale, TimeZone timeZone) {
  3. if (timeZone == null || value == null) {
  4. return format(value, locale);
  5. }
  6. OffsetDateTime offsetDateTime = (OffsetDateTime) value;
  7. LocalDateTime localDateTime = offsetDateTime.atZoneSameInstant(timeZone.toZoneId()).toLocalDateTime();
  8. return format(localDateTime, locale);
  9. }

代码示例来源:origin: Silverpeas/Silverpeas-Core

  1. /**
  2. * Gets optionally an event occurrence from the specified data.
  3. * @param event an event.
  4. * @param occurrenceStartDate a start date.
  5. * @return the computed occurrence identifier.
  6. */
  7. public static Optional<CalendarEventOccurrence> getBy(CalendarEvent event, Temporal occurrenceStartDate) {
  8. Temporal startDate = occurrenceStartDate;
  9. if (startDate instanceof OffsetDateTime) {
  10. startDate = ((OffsetDateTime) occurrenceStartDate).atZoneSameInstant(ZoneOffset.UTC)
  11. .toOffsetDateTime();
  12. }
  13. return getById(generateId(event, startDate));
  14. }

代码示例来源:origin: org.jadira.usertype/usertype.extended

  1. @Override
  2. public OffsetDateTime fromNonNullValue(Timestamp value) {
  3. ZoneId currentDatabaseZone = getDatabaseZone() == null ? getDefaultZoneId() : getDatabaseZone();
  4. ZoneId currentJavaZone = javaZone == null ? getDefaultZoneId() : javaZone;
  5. OffsetDateTime dateTime = OffsetDateTime.ofInstant(Instant.ofEpochMilli(value.getTime()), currentDatabaseZone);
  6. ZonedDateTime zonedDateTime = dateTime.with(ChronoField.NANO_OF_SECOND, value.getNanos()).atZoneSameInstant(currentJavaZone);
  7. dateTime = zonedDateTime.toOffsetDateTime();
  8. return dateTime;
  9. }

代码示例来源:origin: org.jadira.usertype/usertype.extended

  1. @Override
  2. public OffsetTime fromNonNullValue(Timestamp value) {
  3. ZoneId currentDatabaseZone = getDatabaseZone() == null ? getDefaultZoneOffset() : getDatabaseZone();
  4. ZoneId currentJavaZone = javaZone == null ? getDefaultZoneOffset() : javaZone;
  5. OffsetDateTime dateTime = OffsetDateTime.ofInstant(Instant.ofEpochMilli(value.getTime()), currentDatabaseZone);
  6. dateTime = dateTime.with(ChronoField.NANO_OF_SECOND, value.getNanos()).atZoneSameInstant(currentJavaZone).toOffsetDateTime();
  7. OffsetTime time = dateTime.toOffsetTime();
  8. return time;
  9. }

代码示例来源:origin: org.hibernate.orm/hibernate-core

  1. public Calendar fromString(String string) {
  2. final OffsetTime parsedOffsetTime = OffsetTime.parse( string, FORMATTER );
  3. return GregorianCalendar.from( parsedOffsetTime.atDate( LocalDate.MIN ).atZoneSameInstant( parsedOffsetTime.getOffset() ) );
  4. }

代码示例来源:origin: Silverpeas/Silverpeas-Core

  1. /**
  2. * Triggers this reminder at the specified date time. The timezone of the specified date time
  3. * will be set in the timezone of the user behind this reminder.
  4. * @param dateTime the date time at which this reminder will be triggered once scheduled.
  5. * @return itself.
  6. */
  7. public DateTimeReminder triggerAt(final OffsetDateTime dateTime) {
  8. final ZoneId userZoneId = User.getById(getUserId()).getUserPreferences().getZoneId();
  9. this.dateTime = dateTime.atZoneSameInstant(userZoneId).toOffsetDateTime();
  10. return this;
  11. }

代码示例来源:origin: marschall/threeten-jpa

  1. /**
  2. * Converts {@link OffsetDateTime} to {@link TIMESTAMPTZ}.
  3. *
  4. * @param attribute the value to be converted, possibly {@code null}
  5. * @return the converted data, possibly {@code null}
  6. */
  7. public static TIMESTAMPTZ offsetDateTimeToTimestamptz(OffsetDateTime attribute) {
  8. if (attribute == null) {
  9. return null;
  10. }
  11. byte[] bytes = newTimestamptzBuffer();
  12. ZonedDateTime utc = attribute.atZoneSameInstant(UTC);
  13. writeDateTime(bytes, utc.toLocalDateTime());
  14. ZoneOffset offset = attribute.getOffset();
  15. writeZoneOffset(bytes, offset);
  16. return new TIMESTAMPTZ(bytes);
  17. }

代码示例来源:origin: com.github.marschall/threeten-jpa-oracle-impl

  1. /**
  2. * Converts {@link OffsetDateTime} to {@link TIMESTAMPTZ}.
  3. *
  4. * @param attribute the value to be converted, possibly {@code null}
  5. * @return the converted data, possibly {@code null}
  6. */
  7. public static TIMESTAMPTZ offsetDateTimeToTimestamptz(OffsetDateTime attribute) {
  8. if (attribute == null) {
  9. return null;
  10. }
  11. byte[] bytes = newTimestamptzBuffer();
  12. ZonedDateTime utc = attribute.atZoneSameInstant(UTC);
  13. writeDateTime(bytes, utc.toLocalDateTime());
  14. ZoneOffset offset = attribute.getOffset();
  15. writeZoneOffset(bytes, offset);
  16. return new TIMESTAMPTZ(bytes);
  17. }

代码示例来源:origin: arnaudroger/SimpleFlatMapper

  1. @Test
  2. public void testObjectToOffsetDateTime() throws Exception {
  3. ZoneId zoneId = ZoneId.systemDefault();
  4. OffsetDateTime offsetDateTime = OffsetDateTime.now(zoneId);
  5. testObjectToOffsetDateTime(null, null);
  6. testObjectToOffsetDateTime(offsetDateTime, offsetDateTime);
  7. testObjectToOffsetDateTime(offsetDateTime.toLocalDateTime(), offsetDateTime);
  8. testObjectToOffsetDateTime(offsetDateTime.toInstant(), offsetDateTime);
  9. testObjectToOffsetDateTime(offsetDateTime.atZoneSameInstant(zoneId), offsetDateTime);
  10. testObjectToOffsetDateTime(offsetDateTime.atZoneSameInstant(zoneId), offsetDateTime);
  11. testObjectToOffsetDateTime(offsetDateTime.toLocalDate(), offsetDateTime.truncatedTo(ChronoUnit.DAYS));
  12. testObjectToOffsetDateTime(Date.from(offsetDateTime.toInstant()), offsetDateTime.truncatedTo(ChronoUnit.MILLIS));
  13. try {
  14. testObjectToOffsetDateTime("a string", offsetDateTime);
  15. fail();
  16. } catch (IllegalArgumentException e) {
  17. // expected
  18. }
  19. }

相关文章