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

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

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

OffsetDateTime.withOffsetSameInstant介绍

[英]Returns a copy of this OffsetDateTime with the specified offset ensuring that the result is at the same instant.

This method returns an object with the specified ZoneOffset and a LocalDateTimeadjusted by the difference between the two offsets. This will result in the old and new objects representing the same instant. This is useful for finding the local time in a different offset. For example, if this time represents 2007-12-03T10:30+02:00 and the offset specified is +03:00, then this method will return 2007-12-03T11:30+03:00.

To change the offset without adjusting the local time use #withOffsetSameLocal.

This instance is immutable and unaffected by this method call.
[中]返回具有指定偏移量的此OffsetDateTime的副本,确保结果在同一时刻。
此方法返回具有指定ZoneOffset和LocalDateTimeadjusted(由两个偏移量之间的差值调整)的对象。这将导致新旧对象代表同一时刻。这对于在不同偏移中查找本地时间很有用。例如,如果此时间表示2007-12-03T10:30+02:00,且指定的偏移量为+03:00,则此方法将返回2007-12-03T11:30+03:00。
要更改偏移量而不调整本地时间,请使用#withOffsetSameLocal。
此实例是不可变的,不受此方法调用的影响。

代码示例

代码示例来源:origin: jdbi/jdbi

@Test
public void offsetDateTimeLosesOffset() {
  OffsetDateTime dt = OffsetDateTime.now().withOffsetSameInstant(ZoneOffset.ofHours(-7));
  h.execute("insert into stuff(ts) values (?)", dt);
  assertThat(h.createQuery("select ts from stuff").mapTo(OffsetDateTime.class).findOnly().isEqual(dt)).isTrue();
}

代码示例来源:origin: jdbi/jdbi

@Test
public void offsetDateTimeLosesOffset() {
  OffsetDateTime dt = OffsetDateTime.now().withOffsetSameInstant(ZoneOffset.ofHours(-7));
  h.execute("insert into stuff(ts) values (?)", dt);
  assertThat(dt.isEqual(h.createQuery("select ts from stuff").mapTo(OffsetDateTime.class).findOnly())).isTrue();
}

代码示例来源:origin: jdbi/jdbi

@Test
public void offsetDateTime() {
  OffsetDateTime dt = OffsetDateTime.now().withOffsetSameInstant(ZoneOffset.UTC);
  h.execute("insert into stuff(ts) values (?)", dt);
  assertThat(h.createQuery("select ts from stuff").mapTo(OffsetDateTime.class).findOnly()).isEqualTo(dt);
}

代码示例来源:origin: debezium/debezium

c4DateTime = c4DateTime.withOffsetSameInstant(ZoneOffset.of("Z"));
assertThat(c4DateTime.getYear()).isEqualTo(1970);
assertThat(c4DateTime.getMonth()).isEqualTo(Month.JANUARY);

代码示例来源:origin: bonitasoft/bonita-engine

@Override
  public void serialize(OffsetDateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    jgen.writeString((value != null) ? DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(value.withOffsetSameInstant(ZoneOffset.UTC)) : null);
  }
}

代码示例来源:origin: bonitasoft/bonita-engine

@Override
  public void serialize(OffsetDateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    jgen.writeString((value != null) ? DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(value.withOffsetSameInstant(ZoneOffset.UTC)) : null);
  }
}

代码示例来源:origin: net.dongliu/xhttp

/**
 * Create new header, with datetime value
 */
public static Header ofDate(String name, OffsetDateTime dateTime) {
  requireNonNull(name);
  requireNonNull(dateTime);
  return new Header(name, RFC_1123_DATE_TIME.format(dateTime.withOffsetSameInstant(UTC)));
}

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

DateTimeFormatter fmt = new DateTimeFormatterBuilder()
  .append(DateTimeFormatter.ISO_OFFSET_DATE)
  .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
  .toFormatter();
OffsetDateTime dt = OffsetDateTime.parse("2015-01-15-05:00", fmt);
LocalDate date = dt.withOffsetSameInstant(ZoneOffset.UTC).toLocalDate();

代码示例来源:origin: bonitasoft/bonita-engine

@Override
public String convertToDatabaseColumn(OffsetDateTime offsetDateTime) {
  if (offsetDateTime != null) {
    return offsetDateTime.withOffsetSameInstant(ZoneOffset.UTC).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
  } else {
    return null;
  }
}

代码示例来源:origin: bonitasoft/bonita-engine

public Object fromString(String str) {
    try {
      return OffsetDateTime.parse(str).withOffsetSameInstant(ZoneOffset.UTC);
    } catch (DateTimeParseException e) {
      throw new RuntimeException("OffsetDateTime failed to parse the incoming string", e);
    }
  }
}

代码示例来源:origin: bonitasoft/bonita-engine

@Override
public String convertToDatabaseColumn(OffsetDateTime offsetDateTime) {
  if (offsetDateTime != null) {
    return offsetDateTime.withOffsetSameInstant(ZoneOffset.UTC).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
  } else {
    return null;
  }
}

代码示例来源:origin: bonitasoft/bonita-engine

@Override
  public OffsetDateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    final String value = jp.readValueAs(String.class);
    if (value == null) {
      return null;
    }
    return OffsetDateTime.parse(value).withOffsetSameInstant(ZoneOffset.UTC);
  }
}

代码示例来源:origin: bonitasoft/bonita-engine

public Object fromString(String str) {
    try {
      return OffsetDateTime.parse(str).withOffsetSameInstant(ZoneOffset.UTC);
    } catch (DateTimeParseException e) {
      throw new RuntimeException("OffsetDateTime failed to parse the incoming string", e);
    }
  }
}

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

@Override
protected OffsetDateTime toUtc(OffsetDateTime dateTime) {
  if (this.isUtc()) {
    if (dateTime == null) {
      return null;
    }
    return dateTime.withOffsetSameInstant(INSTANT_ZONE_OFFSET);
  } else {
    return dateTime;
  }
}

代码示例来源:origin: de.juplo/jpa-converters

@Override
public Timestamp convertToDatabaseColumn(OffsetDateTime odt)
{
 if (odt == null)
  return null;
 return Timestamp.valueOf(odt.withOffsetSameInstant(TimeConversions.getZoneOffset()).toLocalDateTime());
}

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

/**
 * Is this period starts at the the minimum supported date/datetime in Java?
 * @return true if this period starts at the minimum date/datetime supported by Java.
 * False otherwise.
 * @see LocalDate#MIN for the minimum supported date.
 * @see OffsetDateTime#MIN for the maximum supported date.
 */
public boolean startsAtMinDate() {
 return startDateTime.withOffsetSameInstant(OffsetDateTime.MIN.getOffset())
   .equals(OffsetDateTime.MIN);
}

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

/**
 * Is this period ends at the the maximum supported date/datetime in Java?
 * @return true if this period ends at the minimum date/datetime supported by Java.
 * False otherwise.
 * @see LocalDate#MAX for the maximum supported datetime.
 * @see OffsetDateTime#MAX for the maximum supported datetime.
 */
public boolean endsAtMaxDate() {
 return endDateTime.withOffsetSameInstant(OffsetDateTime.MAX.getOffset())
   .equals(OffsetDateTime.MAX);
}

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

@Override
public Timestamp convertToDatabaseColumn(OffsetDateTime dateTime) {
 if (dateTime == null) {
  return null;
 }
 if (dateTime.equals(OffsetDateTime.MIN)) {
  return MIN_TIMESTAMP;
 }
 if (dateTime.equals(OffsetDateTime.MAX)) {
  return MAX_TIMESTAMP;
 }
 return Timestamp.valueOf(dateTime.withOffsetSameInstant(ZoneOffset.UTC).toLocalDateTime());
}

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

public static OffsetDateTime dateTimeOffsetToOffsetDateTime(DateTimeOffset dbData) {
 if (dbData == null) {
  return null;
 }
 OffsetDateTime utc = OffsetDateTime.ofInstant(dbData.getTimestamp().toInstant(), UTC);
 int offsetSeconds = Math.toIntExact(MINUTES.toSeconds(dbData.getMinutesOffset()));
 ZoneOffset offset = ZoneOffset.ofTotalSeconds(offsetSeconds);
 return utc.withOffsetSameInstant(offset);
}

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

public static OffsetDateTime dateTimeOffsetToOffsetDateTime(DateTimeOffset dbData) {
 if (dbData == null) {
  return null;
 }
 OffsetDateTime utc = OffsetDateTime.ofInstant(dbData.getTimestamp().toInstant(), UTC);
 int offsetSeconds = Math.toIntExact(MINUTES.toSeconds(dbData.getMinutesOffset()));
 ZoneOffset offset = ZoneOffset.ofTotalSeconds(offsetSeconds);
 return utc.withOffsetSameInstant(offset);
}

相关文章