gpt4 book ai didi

java.time.ZoneId.from()方法的使用及代码示例

转载 作者:知者 更新时间:2024-03-17 17:12:40 26 4
gpt4 key购买 nike

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

ZoneId.from介绍

[英]Obtains an instance of ZoneId from a temporal object.

A TemporalAccessor represents some form of date and time information. This factory converts the arbitrary temporal object to an instance of ZoneId.

The conversion will try to obtain the zone in a way that favours region-based zones over offset-based zones using TemporalQueries#zone().

This method matches the signature of the functional interface TemporalQueryallowing it to be used in queries via method reference, ZoneId::from.
[中]从时间对象获取ZoneId的实例。
临时助理代表某种形式的日期和时间信息。该工厂将任意时态对象转换为ZoneId的实例。
转换将尝试使用TemporalQueries#zone()以支持基于区域的分区而不是基于偏移的分区的方式获取分区。
此方法匹配函数接口TemporalQueryLow的签名,通过方法引用ZoneId::from将其用于查询。

代码示例

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

return packDateTimeWithZone(parseResult.getLong(INSTANT_SECONDS) * 1000 + parseResult.getLong(MILLI_OF_SECOND), getTimeZoneKey(ZoneId.from(parseResult).getId()));
return packDateTimeWithZone(parseResult.get(MILLI_OF_DAY), getTimeZoneKey(ZoneId.from(parseResult).getId()));

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

zoneId = ZoneId.from(accessor);
} catch (DateTimeException e2) {
 if (defaultTimeZone == null) {

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

@Override
public String format(TemporalAccessor accessor) {
  DateTimeZone timeZone = DateUtils.zoneIdToDateTimeZone(ZoneId.from(accessor));
  DateTime dateTime = new DateTime(Instant.from(accessor).toEpochMilli(), timeZone);
  return printer.print(dateTime);
}

代码示例来源:origin: com.github.seratch/java-time-backport

@Override
  public ZoneId queryFrom(TemporalAccessor temporal) {
    return ZoneId.from(temporal);
  }
};

代码示例来源:origin: cn.vertxup/vertx-co

private static ZoneId getAdjust(final String literal) {
  if (literal.endsWith("Z")) {
    return ZoneId.from(ZoneOffset.UTC);
  } else {
    return ZoneId.systemDefault();
  }
}

代码示例来源:origin: silentbalanceyh/vertx-zero

private static ZoneId getAdjust(final String literal) {
  if (literal.endsWith("Z")) {
    return ZoneId.from(ZoneOffset.UTC);
  } else {
    return ZoneId.systemDefault();
  }
}

代码示例来源:origin: bazaarvoice/emodb

@Override
  protected Instant parse(String input) throws Exception {
    if (CharMatcher.DIGIT.matchesAllOf(input)) {
      // Assume millis, but check for nanos
      long millis = Long.parseLong(input);
      if (millis >= 253402300800000L) { // +10000-01-01T00:00:00Z
        millis = TimeUnit.NANOSECONDS.toMillis(millis);
      }
      return Instant.ofEpochMilli(millis);
    }
    try {
      return ZonedDateTime.parse(input).toInstant();
    } catch (DateTimeParseException e) {
      TemporalAccessor ta = RFC_822_FORMAT.parse(input);
      LocalDateTime dateTime = LocalDateTime.from(ta);
      return ZonedDateTime.of(dateTime, ta.isSupported(ChronoField.OFFSET_SECONDS) ? ZoneId.from(ta) : ZoneOffset.UTC).toInstant();
    }
  }
}

代码示例来源:origin: netzwerg/paleo

private static Month toMonth(Instant instant) {
  return instant.atZone(ZoneId.from(ZoneOffset.UTC)).getMonth();
}

代码示例来源:origin: org.fcrepo/modeshape-common

/**
 * Creates a {@link ZonedDateTime} instance based on the given pattern in ISO 8601 format, compatible with the Joda date-time
 * library. 
 * <p>
 * Note that there is no direct correspondence between the JODA-style dates and the new JDK 8 date, especially 
 * when it comes to handling milliseconds.
 * </p>
 * 
 * @param iso8601 a {@link String} representing a date and/or time pattern, may not be null
 * @return a {@link ZonedDateTime} instance, never {@code null}
 * 
 * @throws java.time.format.DateTimeParseException if the given pattern cannot be parsed
 */
public static ZonedDateTime jodaParse( String iso8601 ) throws DateTimeParseException {
  CheckArg.isNotNull(iso8601, "iso8601");
  TemporalAccessor parse = JODA_ISO8601_FORMATTER.parse(iso8601);
  LocalDate localDate = LocalDate.from(parse);
  LocalTime localTime = parse.isSupported(ChronoField.HOUR_OF_DAY) ? LocalTime.from(parse) : LocalTime.MIDNIGHT;
  ZoneId zoneId = parse.isSupported(ChronoField.OFFSET_SECONDS) ? ZoneId.from(parse) : UTC;
  return ZonedDateTime.of(localDate, localTime, zoneId);    
}

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

ZoneId zone = ZoneId.from( temporal );
ZoneOffset offset = ZoneOffset.from( temporal );
LocalDateTime ldt = LocalDateTime.from( temporal );

代码示例来源:origin: org.modeshape/modeshape-common

/**
 * Creates a {@link ZonedDateTime} instance based on the given pattern in ISO 8601 format, compatible with the Joda date-time
 * library. 
 * <p>
 * Note that there is no direct correspondence between the JODA-style dates and the new JDK 8 date, especially 
 * when it comes to handling milliseconds.
 * </p>
 * 
 * @param iso8601 a {@link String} representing a date and/or time pattern, may not be null
 * @return a {@link ZonedDateTime} instance, never {@code null}
 * 
 * @throws java.time.format.DateTimeParseException if the given pattern cannot be parsed
 */
public static ZonedDateTime jodaParse( String iso8601 ) throws DateTimeParseException {
  CheckArg.isNotNull(iso8601, "iso8601");
  TemporalAccessor parse = JODA_ISO8601_FORMATTER.parse(iso8601);
  LocalDate localDate = LocalDate.from(parse);
  LocalTime localTime = parse.isSupported(ChronoField.HOUR_OF_DAY) ? LocalTime.from(parse) : LocalTime.MIDNIGHT;
  ZoneId zoneId = parse.isSupported(ChronoField.OFFSET_SECONDS) ? ZoneId.from(parse) : UTC;
  return ZonedDateTime.of(localDate, localTime, zoneId);    
}

代码示例来源:origin: org.infinispan/infinispan-embedded-query

ZoneId zone = ZoneId.from( temporal );
ZoneOffset offset = ZoneOffset.from( temporal );
LocalDateTime ldt = LocalDateTime.from( temporal );

代码示例来源:origin: ModeShape/modeshape

/**
 * Creates a {@link ZonedDateTime} instance based on the given pattern in ISO 8601 format, compatible with the Joda date-time
 * library. 
 * <p>
 * Note that there is no direct correspondence between the JODA-style dates and the new JDK 8 date, especially 
 * when it comes to handling milliseconds.
 * </p>
 * 
 * @param iso8601 a {@link String} representing a date and/or time pattern, may not be null
 * @return a {@link ZonedDateTime} instance, never {@code null}
 * 
 * @throws java.time.format.DateTimeParseException if the given pattern cannot be parsed
 */
public static ZonedDateTime jodaParse( String iso8601 ) throws DateTimeParseException {
  CheckArg.isNotNull(iso8601, "iso8601");
  TemporalAccessor parse = JODA_ISO8601_FORMATTER.parse(iso8601);
  LocalDate localDate = LocalDate.from(parse);
  LocalTime localTime = parse.isSupported(ChronoField.HOUR_OF_DAY) ? LocalTime.from(parse) : LocalTime.MIDNIGHT;
  ZoneId zoneId = parse.isSupported(ChronoField.OFFSET_SECONDS) ? ZoneId.from(parse) : UTC;
  return ZonedDateTime.of(localDate, localTime, zoneId);    
}

代码示例来源:origin: com.github.seratch/java-time-backport

ZoneId zone = ZoneId.from(temporal);
if (temporal.isSupported(INSTANT_SECONDS)) {
  try {

代码示例来源:origin: org.apache.hive/hive-common

zoneId = ZoneId.from(accessor);
} catch (DateTimeException e2) {
 if (defaultTimeZone == null) {

代码示例来源:origin: io.prestosql/presto-record-decoder

return packDateTimeWithZone(parseResult.getLong(INSTANT_SECONDS) * 1000 + parseResult.getLong(MILLI_OF_SECOND), getTimeZoneKey(ZoneId.from(parseResult).getId()));
return packDateTimeWithZone(parseResult.get(MILLI_OF_DAY), getTimeZoneKey(ZoneId.from(parseResult).getId()));

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

return packDateTimeWithZone(parseResult.getLong(INSTANT_SECONDS) * 1000 + parseResult.getLong(MILLI_OF_SECOND), getTimeZoneKey(ZoneId.from(parseResult).getId()));
return packDateTimeWithZone(parseResult.get(MILLI_OF_DAY), getTimeZoneKey(ZoneId.from(parseResult).getId()));

代码示例来源:origin: com.facebook.presto/presto-record-decoder

return packDateTimeWithZone(parseResult.getLong(INSTANT_SECONDS) * 1000 + parseResult.getLong(MILLI_OF_SECOND), getTimeZoneKey(ZoneId.from(parseResult).getId()));
return packDateTimeWithZone(parseResult.get(MILLI_OF_DAY), getTimeZoneKey(ZoneId.from(parseResult).getId()));

代码示例来源:origin: com.github.seratch/java-time-backport

/**
 * Obtains a zoned date-time in this chronology from another temporal object.
 * <p>
 * This creates a date-time in this chronology based on the specified {@code TemporalAccessor}.
 * <p>
 * This should obtain a {@code ZoneId} using {@link ZoneId#from(TemporalAccessor)}.
 * The date-time should be obtained by obtaining an {@code Instant}.
 * If that fails, the local date-time should be used.
 *
 * @param temporal  the temporal object to convert, not null
 * @return the zoned date-time in this chronology, not null
 * @throws DateTimeException if unable to create the date-time
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public ChronoZonedDateTime<?> zonedDateTime(TemporalAccessor temporal) {
  try {
    ZoneId zone = ZoneId.from(temporal);
    try {
      Instant instant = Instant.from(temporal);
      return zonedDateTime(instant, zone);
    } catch (DateTimeException ex1) {
      ChronoLocalDateTime cldt = localDateTime(temporal);
      ChronoLocalDateTimeImpl cldtImpl = ensureChronoLocalDateTime(cldt);
      return ChronoZonedDateTimeImpl.ofBest(cldtImpl, zone, null);
    }
  } catch (DateTimeException ex) {
    throw new DateTimeException("Unable to obtain ChronoZonedDateTime from TemporalAccessor: " + temporal.getClass(), ex);
  }
}

代码示例来源:origin: org.kie/kie-dmn-feel

public FEELFnResult<TemporalAccessor> invoke(@ParameterName( "date" ) TemporalAccessor date, @ParameterName( "time" ) TemporalAccessor time) {
  if ( date == null ) {
    return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "date", "cannot be null"));
  }
  if ( !(date instanceof LocalDate) ) {
    // FEEL Spec Table 58 "date is a date or date time [...] creates a date time from the given date (ignoring any time component)" [that means ignoring any TZ from `date` parameter, too]
    // I try to convert `date` to a LocalDate, if the query method returns null would signify conversion is not possible.
    date = date.query(TemporalQueries.localDate());
    if (date == null) {
      return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "date", "must be an instance of LocalDate (or must be possible to convert to a FEEL date using built-in date(date) )"));
    }
  }
  if ( time == null ) {
    return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "time", "cannot be null"));
  }
  if (!(time instanceof LocalTime || (time.query(TemporalQueries.localTime()) != null && time.query(TemporalQueries.zone()) != null))) {
    return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "time", "must be an instance of LocalTime or (it must contain localTime AND zone)"));
  }
  try {
    if( date instanceof LocalDate && time instanceof LocalTime ) {
      return FEELFnResult.ofResult( LocalDateTime.of( (LocalDate) date, (LocalTime) time ) );
    } else if (date instanceof LocalDate && (time.query(TemporalQueries.localTime()) != null && time.query(TemporalQueries.zone()) != null)) {
      return FEELFnResult.ofResult(ZonedDateTime.of((LocalDate) date, LocalTime.from(time), ZoneId.from(time)));
    }
    return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "cannot invoke function for the input parameters"));
  } catch (DateTimeException e) {
    return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "input parameters date-parsing exception", e));
  }
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com