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

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

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

OffsetDateTime.ofInstant介绍

[英]Obtains an instance of OffsetDateTime from an Instant and zone ID.

This creates an offset date-time with the same instant as that specified. Finding the offset from UTC/Greenwich is simple as there is only one valid offset for each instant.
[中]从即时和区域ID获取OffsetDateTime的实例。
这将创建一个与指定时间相同的偏移日期时间。查找UTC/格林威治的偏移量很简单,因为每个瞬间只有一个有效偏移量。

代码示例

代码示例来源:origin: eclipse-vertx/vert.x

private static OffsetDateTime fromMillis(long epochMillis) {
  return OffsetDateTime.ofInstant(Instant.ofEpochMilli(epochMillis), ZoneId.systemDefault());
 }
}

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

@Nullable
  private OffsetDateTime convertToOffsetDateTime(Timestamp timestamp) {
    if (timestamp == null) {
      return null;
    }
    final Optional<ZoneId> zoneId = calendar.flatMap(c -> Optional.of(c.getTimeZone().toZoneId()));
    return OffsetDateTime.ofInstant(
      Instant.ofEpochSecond(timestamp.getTime() / 1000, timestamp.getNanos()),
      zoneId.orElse(ZoneId.systemDefault()));
  }
}

代码示例来源:origin: SonarSource/sonarqube

/**
 * Warning: relies on default timezone!
 */
public static String formatDateTime(long ms) {
 return formatDateTime(OffsetDateTime.ofInstant(Instant.ofEpochMilli(ms), ZoneId.systemDefault()));
}

代码示例来源:origin: SonarSource/sonarqube

/**
 * Warning: relies on default timezone!
 */
public static String formatDateTime(Date d) {
 return formatDateTime(OffsetDateTime.ofInstant(d.toInstant(), ZoneId.systemDefault()));
}

代码示例来源:origin: DozerMapper/dozer

private OffsetDateTime convertToOffsetDateTime(Object srcObject) {
    Class<?> srcObjectClass = srcObject.getClass();
    if (Date.class.isAssignableFrom(srcObjectClass)) {
      Instant instant = ((Date)srcObject).toInstant();
      return OffsetDateTime.ofInstant(instant, ZoneId.systemDefault());
    } else if (Instant.class.isAssignableFrom(srcObjectClass)) {
      return OffsetDateTime.ofInstant((Instant)srcObject, ZoneId.systemDefault());
    } else if (Long.class.isAssignableFrom(srcObjectClass)) {
      Instant instant = Instant.ofEpochMilli((Long)srcObject);
      return OffsetDateTime.ofInstant(instant, ZoneId.systemDefault());
    } else {
      return null;
    }
  }
}

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

@Override
  public String toString(final Object obj) {
    final Calendar calendar = (Calendar)obj;
    final Instant instant = Instant.ofEpochMilli(calendar.getTimeInMillis());
    final int offsetInMillis = calendar.getTimeZone().getOffset(calendar.getTimeInMillis());
    final OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(instant, ZoneOffset.ofTotalSeconds(offsetInMillis
      / 1000));
    return STD_DATE_TIME.format(offsetDateTime);
  }
}

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

@Override
public String format(final Timestamp value) {
  return this.formatter.format(OffsetDateTime.ofInstant(value.getInstant(), this.zoneOffset));
}

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

private static OffsetDateTime getOffsetDateTime(ResultSet r, int i) throws SQLException {
  Timestamp ts = r.getTimestamp(i);
  return ts == null ? null : OffsetDateTime.ofInstant(ts.toInstant(), ZoneId.systemDefault());
}

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

return OffsetDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() );
return OffsetDateTime.ofInstant( date.toInstant(), ZoneId.systemDefault() );
return OffsetDateTime.ofInstant( Instant.ofEpochMilli( (Long) value ), ZoneId.systemDefault() );
return OffsetDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() );

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

@Override
  public OffsetDateTime convertToMapped(Class<? extends OffsetDateTime> type, Timestamp value) {
    if (value == null) {
      return null;
    }
    return OffsetDateTime.ofInstant(value.toInstant(), ZoneOffset.systemDefault());
  }
}

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

@Override
protected Object convertTimestampWithZone(Column column, Field fieldDefn, Object data) {
  if (data instanceof Long) {
    LocalDateTime localDateTime = nanosToLocalDateTimeUTC((Long) data);
    data = OffsetDateTime.of(localDateTime, ZoneOffset.UTC);
  } else if (data instanceof java.util.Date) {
    // any Date like subclasses will be given to us by the JDBC driver, which uses the local VM TZ, so we need to go
    // back to GMT
    data = OffsetDateTime.ofInstant(Instant.ofEpochMilli(((Date) data).getTime()), ZoneOffset.UTC);
  }
  return super.convertTimestampWithZone(column, fieldDefn, data);
}

代码示例来源:origin: DV8FromTheWorld/JDA

@Override
public OffsetDateTime getJoinDate()
{
  return OffsetDateTime.ofInstant(Instant.ofEpochMilli(joinDate), OFFSET);
}

代码示例来源:origin: org.postgresql/postgresql

OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(timestampValue.toInstant(), ZoneOffset.UTC);
 return type.cast(offsetDateTime);
} else {

代码示例来源:origin: ebean-orm/ebean

/**
 * Return the system millis time as a OffsetDateTime.
 */
public static Object toOffsetDateTime(long systemMillis) {
 return OffsetDateTime.ofInstant(Instant.ofEpochMilli(systemMillis), ZoneId.systemDefault());
}

代码示例来源:origin: ebean-orm/ebean

@Override
public OffsetDateTime convertFromTimestamp(Timestamp ts) {
 return OffsetDateTime.ofInstant(ts.toInstant(), ZoneId.systemDefault());
}

代码示例来源:origin: apache/cloudstack

@Test
public void zonedTimeFormatIsoOffsetDateTimeMs() throws ParseException {
  Instant moment = Instant.now();
  Date time = Date.from(moment);
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSX");
  String str = OffsetDateTime.ofInstant(moment, ZoneId.systemDefault()).format(formatter);
  Date dtParsed = DateUtil.parseTZDateString(str);
  assertEquals(str, time.toString(), dtParsed.toString());
}

代码示例来源:origin: apache/cloudstack

@Test
  public void zonedTimeFormatIsoNoColonZMs() throws ParseException {
    Instant moment = Instant.now();
    Date time = Date.from(moment);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");
    String str = OffsetDateTime.ofInstant(moment, ZoneId.systemDefault()).format(formatter);

    Date dtParsed = DateUtil.parseTZDateString(str);

    assertEquals(str, time.toString(), dtParsed.toString());
  }
}

代码示例来源:origin: apache/cloudstack

@Test
public void zonedTimeFormatIsoInstantMs() throws ParseException {
  Instant moment = Instant.now();
  Date time = Date.from(moment);
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");
  String str = OffsetDateTime.ofInstant(moment, ZoneId.of("UTC")).format(formatter);
  Date dtParsed = DateUtil.parseTZDateString(str);
  assertEquals(str, time.toString(), dtParsed.toString());
}

代码示例来源:origin: apache/cloudstack

@Test
public void zonedTimeFormatIsoInstant() throws ParseException {
  Instant moment = Instant.now();
  Date time = Date.from(moment);
  String str = OffsetDateTime.ofInstant(moment, ZoneId.systemDefault()).format(DateTimeFormatter.ISO_INSTANT);
  Date dtParsed = DateUtil.parseTZDateString(str);
  assertEquals(str, time.toString(), dtParsed.toString());
}

代码示例来源:origin: apache/cloudstack

@Test
public void zonedTimeFormatIsoOffsetDateTime() throws ParseException {
  Instant moment = Instant.now();
  Date time = Date.from(moment);
  String str = OffsetDateTime.ofInstant(moment, ZoneId.systemDefault()).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
  Date dtParsed = DateUtil.parseTZDateString(str);
  assertEquals(str, time.toString(), dtParsed.toString());
}

相关文章