java.time.ZonedDateTime.toInstant()方法的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(8.0k)|赞(0)|评价(0)|浏览(280)

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

ZonedDateTime.toInstant介绍

暂无

代码示例

代码示例来源:origin: prestodb/presto

protected ZonedDateTimeWithZoneIdSerializer() {
  super(ZonedDateTime.class, dt -> dt.toInstant().toEpochMilli(),
      ZonedDateTime::toEpochSecond, ZonedDateTime::getNano,
      // Serialize in a backwards compatible way: with zone id, using toString method
      null);
}

代码示例来源:origin: lets-blade/blade

public Date format(String date, String pattern) {
  DateTimeFormatter fmt       = DateTimeFormatter.ofPattern(pattern, Locale.US);
  LocalDateTime     formatted = LocalDateTime.parse(date, fmt);
  Instant           instant   = formatted.atZone(ZoneId.systemDefault()).toInstant();
  return Date.from(instant);
}

代码示例来源:origin: lets-blade/blade

/**
 * format string time to unix time
 *
 * @param time    string date
 * @param pattern date format pattern
 * @return return unix time
 */
public static int toUnix(String time, String pattern) {
  LocalDateTime formatted = LocalDateTime.parse(time, DateTimeFormatter.ofPattern(pattern));
  return (int) formatted.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
}

代码示例来源:origin: oblac/jodd

/**
 * Converts local date time to epoh milliseconds.
 */
public static long toMilliseconds(final LocalDateTime localDateTime) {
  return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}

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

Instant instant = value.atZone( ZoneId.systemDefault() ).toInstant();
return (X) java.sql.Timestamp.from( instant );
Instant instant = value.atZone( ZoneId.systemDefault() ).toInstant();
return (X) java.sql.Date.from( instant );
Instant instant = value.atZone( ZoneId.systemDefault() ).toInstant();
return (X) java.sql.Time.from( instant );
Instant instant = value.atZone( ZoneId.systemDefault() ).toInstant();
return (X) java.util.Date.from( instant );
Instant instant = value.atZone( ZoneId.systemDefault() ).toInstant();
return (X) Long.valueOf( instant.toEpochMilli() );

代码示例来源:origin: Graylog2/graylog2-server

@Override
  public void serialize(ZonedDateTime zonedDateTime,
             JsonGenerator jsonGenerator,
             SerializerProvider serializerProvider) throws IOException {
    final Instant instant = zonedDateTime.withZoneSameInstant(ZoneOffset.UTC).toInstant();
    final Date date = Date.from(instant);
    jsonGenerator.writeObject(date);
  }
}

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

@Override
@SuppressWarnings("unchecked")
public <X> X unwrap(ZonedDateTime zonedDateTime, Class<X> type, WrapperOptions options) {
  if ( zonedDateTime == null ) {
    return null;
  }
  if ( ZonedDateTime.class.isAssignableFrom( type ) ) {
    return (X) zonedDateTime;
  }
  if ( Calendar.class.isAssignableFrom( type ) ) {
    return (X) GregorianCalendar.from( zonedDateTime );
  }
  if ( Timestamp.class.isAssignableFrom( type ) ) {
    return (X) Timestamp.from( zonedDateTime.toInstant() );
  }
  if ( java.sql.Date.class.isAssignableFrom( type ) ) {
    return (X) java.sql.Date.from( zonedDateTime.toInstant() );
  }
  if ( java.sql.Time.class.isAssignableFrom( type ) ) {
    return (X) java.sql.Time.from( zonedDateTime.toInstant() );
  }
  if ( Date.class.isAssignableFrom( type ) ) {
    return (X) Date.from( zonedDateTime.toInstant() );
  }
  if ( Long.class.isAssignableFrom( type ) ) {
    return (X) Long.valueOf( zonedDateTime.toInstant().toEpochMilli() );
  }
  throw unknownUnwrap( type );
}

代码示例来源:origin: oblac/jodd

/**
 * Converts local date to Date.
 */
public static Date toDate(final LocalDate localDate) {
  return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
}
/**

代码示例来源:origin: prestodb/presto

public ZonedDateTimeSerializer(DateTimeFormatter formatter) {
  super(ZonedDateTime.class, dt -> dt.toInstant().toEpochMilli(),
     ZonedDateTime::toEpochSecond, ZonedDateTime::getNano,
     formatter);
  _writeZoneId = null;
}

代码示例来源:origin: yu199195/hmily

private Date acquireData() {
  return new Date(LocalDateTime.now().atZone(ZoneId.systemDefault())
      .toInstant().toEpochMilli() - (hmilyConfig.getRecoverDelayTime() * 1000));
}

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

@Override
@SuppressWarnings("unchecked")
public <X> X unwrap(LocalDate value, Class<X> type, WrapperOptions options) {
  if ( value == null ) {
    return null;
  }
  if ( LocalDate.class.isAssignableFrom( type ) ) {
    return (X) value;
  }
  if ( java.sql.Date.class.isAssignableFrom( type ) ) {
    return (X) java.sql.Date.valueOf( value );
  }
  final LocalDateTime localDateTime = value.atStartOfDay();
  if ( Timestamp.class.isAssignableFrom( type ) ) {
    return (X) Timestamp.valueOf( localDateTime );
  }
  final ZonedDateTime zonedDateTime = localDateTime.atZone( ZoneId.systemDefault() );
  if ( Calendar.class.isAssignableFrom( type ) ) {
    return (X) GregorianCalendar.from( zonedDateTime );
  }
  final Instant instant = zonedDateTime.toInstant();
  if ( Date.class.equals( type ) ) {
    return (X) Date.from( instant );
  }
  if ( Long.class.isAssignableFrom( type ) ) {
    return (X) Long.valueOf( instant.toEpochMilli() );
  }
  throw unknownUnwrap( type );
}

代码示例来源:origin: prestodb/presto

public String getBearerToken(String subject)
{
  checkState(jwtSigner.isPresent(), "not configured");
  JwtBuilder jwt = Jwts.builder()
      .setSubject(subject)
      .setExpiration(Date.from(ZonedDateTime.now().plusMinutes(5).toInstant()));
  jwtSigner.get().accept(jwt);
  jwtKeyId.ifPresent(keyId -> jwt.setHeaderParam(KEY_ID, keyId));
  jwtIssuer.ifPresent(jwt::setIssuer);
  jwtAudience.ifPresent(jwt::setAudience);
  return jwt.compact();
}

代码示例来源:origin: lets-blade/blade

/**
 * format string time to unix time
 *
 * @param time    string date
 * @param pattern date format pattern
 * @return return unix time
 */
public static int toUnix(String time, String pattern) {
  LocalDateTime formatted = LocalDateTime.parse(time, DateTimeFormatter.ofPattern(pattern));
  return (int) formatted.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
}

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

@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 );
}

代码示例来源:origin: lets-blade/blade

public Date format(String date, String pattern) {
  DateTimeFormatter fmt       = DateTimeFormatter.ofPattern(pattern, Locale.US);
  LocalDateTime     formatted = LocalDateTime.parse(date, fmt);
  Instant           instant   = formatted.atZone(ZoneId.systemDefault()).toInstant();
  return Date.from(instant);
}

代码示例来源:origin: com.fasterxml.jackson.datatype/jackson-datatype-jsr310

protected ZonedDateTimeWithZoneIdSerializer() {
  super(ZonedDateTime.class, dt -> dt.toInstant().toEpochMilli(),
      ZonedDateTime::toEpochSecond, ZonedDateTime::getNano,
      // Serialize in a backwards compatible way: with zone id, using toString method
      null);
}

代码示例来源:origin: yu199195/Raincat

private Date acquireData() {
  return new Date(LocalDateTime.now()
      .atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()
      - (txConfig.getRecoverDelayTime() * 1000));
}

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

final ZonedDateTime zonedDateTime = value.atDate( LocalDate.of( 1970, 1, 1 ) ).atZone( ZoneId.systemDefault() );
final Instant instant = zonedDateTime.toInstant();
  return (X) Date.from( instant );
  return (X) Long.valueOf( instant.toEpochMilli() );

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

private Date getEpochDate() {
    return Date.from(LocalDate.of(1970, 1, 1).atStartOfDay(ZoneId.of("UTC")).toInstant());
  }
}

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

@CheckForNull
private static Instant parseDate(String propertyValue) {
 try {
  LocalDate localDate = LocalDate.parse(propertyValue);
  return localDate.atStartOfDay(ZoneId.systemDefault()).toInstant();
 } catch (DateTimeParseException e) {
  boolean invalidDate = e.getCause() == null || e.getCause() == e || !e.getCause().getMessage().contains("Invalid date");
  checkPeriodProperty(invalidDate, propertyValue, "Invalid date");
  return null;
 }
}

相关文章

ZonedDateTime类方法