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

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

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

OffsetDateTime.from介绍

[英]Obtains an instance of OffsetDateTime 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 OffsetDateTime.

The conversion extracts and combines LocalDateTime and ZoneOffset. If that fails it will try to extract and combine Instant and ZoneOffset.

This method matches the signature of the functional interface TemporalQueryallowing it to be used in queries via method reference, OffsetDateTime::from.
[中]从时态对象获取OffsetDateTime的实例。
临时助理代表某种形式的日期和时间信息。该工厂将任意时态对象转换为OffsetDateTime的实例。
转换提取并组合LocalDateTime和ZoneOffset。如果失败,它将尝试提取并组合Instant和ZoneOffset。
此方法匹配函数接口TemporalQueryLow的签名,通过方法引用OffsetDateTime::from将其用于查询。

代码示例

代码示例来源:origin: prontera/spring-cloud-rest-tcc

@Override
  public OffsetDateTime convertFrom(OffsetDateTime zdt, Type<OffsetDateTime> type, MappingContext mappingContext) {
    return OffsetDateTime.from(zdt);
  }
}

代码示例来源:origin: prontera/spring-cloud-rest-tcc

@Override
public OffsetDateTime convertTo(OffsetDateTime zdt, Type<OffsetDateTime> type, MappingContext mappingContext) {
  return OffsetDateTime.from(zdt);
}

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

@Override
public OffsetDateTime fromString(String string) {
  return OffsetDateTime.from( OffsetDateTimeType.FORMATTER.parse( string ) );
}

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

private static CalendarEventModel.CalendarEventTime getEventTime(EventDateTime dateTime) {
 if (dateTime == null) {
  return null;
 }
 OffsetDateTime offsetDateTime;
 if (dateTime.getDate() == null) {
  offsetDateTime = OffsetDateTime.parse(dateTime.getDateTime().toString());
 } else {
  offsetDateTime =
    OffsetDateTime.from(
      LocalDate.parse(dateTime.getDate().toString()).atStartOfDay(ZoneId.of("UTC")));
 }
 return new CalendarEventModel.CalendarEventTime(offsetDateTime, dateTime.getDate() != null);
}

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

return OffsetDateTime.from(given);
} else {
  return OffsetDateTime.of(LocalDateTime.from(given), this.defaultZoneOffset);

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

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

代码示例来源:origin: net.arnx/jsonic

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

代码示例来源:origin: EvoSuite/evosuite

public static OffsetDateTime from(TemporalAccessor temporal) {
  return OffsetDateTime.from(temporal);
}

代码示例来源:origin: agrestio/agrest

private OffsetDateTime fromString(String val) {
  return null != val ? OffsetDateTime.from(DEFAULT_FORMATTER.parse(val)): null;
}

代码示例来源:origin: kiegroup/droolsjbpm-knowledge

@Override
public OffsetDateTime unmarshal( String offsetDateTimeString ) throws Exception {
  if ( offsetDateTimeString == null ) {
    return null;
  }
  try {
    return OffsetDateTime.from( formatter.parse( offsetDateTimeString ) );
  } catch ( DateTimeException e ) {
    throw new IllegalStateException( "Failed to convert string (" + offsetDateTimeString + ") to type ("
        + OffsetDateTime.class.getName() + ")." );
  }
}

代码示例来源:origin: org.kie.soup/kie-soup-commons

@Override
public Object unmarshal( HierarchicalStreamReader reader, UnmarshallingContext context ) {
  String offsetDateTimeString = reader.getValue();
  try {
    return OffsetDateTime.from( formatter.parse( offsetDateTimeString ) );
  } catch ( DateTimeException e ) {
    throw new IllegalStateException( "Failed to convert string (" + offsetDateTimeString + ") to type ("
        + OffsetDateTime.class.getName() + ")." );
  }
}

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

public static <T> T applyByType(java.time.temporal.Temporal temporal,
  Function<LocalDate, T> localDateFunction, Function<LocalDateTime, T> localDateTimeFunction,
  Function<OffsetDateTime, T> offsetDateTimeFunction,
  Function<ZonedDateTime, T> zonedDateTimeFunction) {
 Objects.requireNonNull(temporal);
 Objects.requireNonNull(localDateFunction);
 Objects.requireNonNull(localDateTimeFunction);
 Objects.requireNonNull(zonedDateTimeFunction);
 Objects.requireNonNull(offsetDateTimeFunction);
 if (temporal instanceof LocalDate) {
  return localDateFunction.apply(LocalDate.from(temporal));
 } else if (temporal instanceof OffsetDateTime) {
  return offsetDateTimeFunction.apply(OffsetDateTime.from(temporal));
 } else if (temporal instanceof ChronoZonedDateTime) {
  return zonedDateTimeFunction.apply(ZonedDateTime.from(temporal));
 } else if (temporal instanceof LocalDateTime) {
  return localDateTimeFunction.apply(LocalDateTime.from(temporal));
 } else {
  throw new IllegalArgumentException(
    "Temporal parameters must be both of type LocalDate or OffsetDateTime or ZonedDateTime");
 }
}

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

/**
 * Performs one of the specified functions according to the type of the specified temporal object.
 * The given temporal object is converted before passing it to one of the specified function.
 * @param temporal a temporal object to consume.
 * @param dateFunction a function that works on a {@link LocalDate} instance.
 * @param dateTimeFunction a function that works on a {@link OffsetDateTime} instance.
 * @param <T> the type of the return value.
 * @return T the return value of the function that was applied to the converted temporal object.
 * @throws IllegalArgumentException if date parameters are not both {@link LocalDate} or
 * {@link OffsetDateTime} instances.
 */
public static <T> T applyByType(java.time.temporal.Temporal temporal,
  Function<LocalDate, T> dateFunction, Function<OffsetDateTime, T> dateTimeFunction) {
 Objects.requireNonNull(temporal);
 Objects.requireNonNull(dateFunction);
 Objects.requireNonNull(dateTimeFunction);
 if (temporal instanceof LocalDate) {
  return dateFunction.apply(LocalDate.from(temporal));
 } else if (temporal instanceof OffsetDateTime) {
  return dateTimeFunction.apply(OffsetDateTime.from(temporal).withOffsetSameInstant(ZoneOffset.UTC));
 } else {
  throw new IllegalArgumentException(
    "Temporal parameters must be both of type LocalDate or OffsetDateTime");
 }
}

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

/**
 * Performs one of the specified functions according to the type of the specified temporal object.
 * The given temporal object is converted before passing it to one of the specified function.
 * @param temporal a temporal object to consume.
 * @param dateFunction a function that works on a {@link LocalDate} instance.
 * @param dateTimeFunction a function that works on a {@link OffsetDateTime} instance.
 * @param zonedDateTimeTFunction a function that works on a {@link ZonedDateTime} instance.
 * @param <T> the type of the return value.
 * @return T the return value of the function that was applied to the converted temporal object.
 * @throws IllegalArgumentException if date parameters are not both {@link LocalDate} or
 * {@link OffsetDateTime} instances.
 */
public static <T> T applyByType(java.time.temporal.Temporal temporal,
  Function<LocalDate, T> dateFunction, Function<OffsetDateTime, T> dateTimeFunction,
  Function<ZonedDateTime, T> zonedDateTimeTFunction) {
 Objects.requireNonNull(temporal);
 Objects.requireNonNull(dateFunction);
 Objects.requireNonNull(dateTimeFunction);
 Objects.requireNonNull(zonedDateTimeTFunction);
 if (temporal instanceof LocalDate) {
  return dateFunction.apply(LocalDate.from(temporal));
 } else if (temporal instanceof OffsetDateTime) {
  return dateTimeFunction.apply(OffsetDateTime.from(temporal));
 } else if (temporal instanceof ZonedDateTime) {
  return zonedDateTimeTFunction.apply(ZonedDateTime.from(temporal));
 } else {
  throw new IllegalArgumentException(
    "Temporal parameters must be both of type LocalDate or OffsetDateTime or ZonedDateTime");
 }
}

代码示例来源:origin: OpenWiseSolutions/openhub-framework

@Nullable
public static OffsetDateTime parseDate(@Nullable String dateStr) {
  if (dateStr == null) {
    return null;
  }
  // parsing has two steps:
  //  1) parse date with offset "2013-10-05+02:00"
  //  2) parse date without offset "2013-10-05"
  try {
    return OffsetDateTime.from(ISO_OFFSET_DATE_MIDNIGHT.parse(dateStr));
  } catch (DateTimeParseException ex) {
    TemporalAccessor dt = DateTimeFormatter.ISO_DATE.parseBest(dateStr, OffsetDateTime::from, LocalDate::from);
    if (dt instanceof OffsetDateTime) {
      return (OffsetDateTime) dt;
    } else {
      LocalDateTime ldt = LocalDateTime.of((LocalDate)dt, LocalTime.now());
      return OffsetDateTime.of(ldt.truncatedTo(ChronoUnit.DAYS), ZoneOffset.from(ZonedDateTime.now()));
    }
  }
}

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

/**
 * Creates a new period of time between the two specified non null date or datetime.
 * If date parameters are instances of {@link LocalDate}, take a look at method
 * {@link #between(LocalDate, LocalDate)}.
 * If date parameters are instances of {@link OffsetDateTime}, take a look at method
 * {@link #between(OffsetDateTime, OffsetDateTime)}.
 * @param start the start of the period. It defines the inclusive date or datetime at which the
 * period starts.
 * @param end the end day of the period. It defines the exclusive date or the exclusive datetime
 * at which the period ends. The end date must be the same or after the start date. An end date
 * equal to the start date means the period is spanning all the day; it is equivalent to an end
 * date being one day after the start date.
 * @return the period of days between the two specified dates.
 * @throws IllegalArgumentException if date parameters are not both {@link LocalDate} or
 * {@link OffsetDateTime} instances.
 */
public static Period between(java.time.temporal.Temporal start, java.time.temporal.Temporal end) {
 if (start instanceof LocalDate && end instanceof LocalDate) {
  return between(LocalDate.from(start), LocalDate.from(end));
 } else if (start instanceof OffsetDateTime && end instanceof OffsetDateTime) {
  return between(OffsetDateTime.from(start), OffsetDateTime.from(end));
 } else {
  throw new IllegalArgumentException(
    "Temporal parameters must be either of type LocalDate or OffsetDateTime");
 }
}

代码示例来源:origin: com.impossibl.pgjdbc-ng/pgjdbc-ng

@Override
protected Object decodeValue(Context context, Type type, Short typeLength, Integer typeModifier, CharSequence buffer, Class<?> targetClass, Object targetContext) throws IOException {
 Calendar calendar = targetContext != null ? (Calendar) targetContext : Calendar.getInstance();
 if (buffer.equals(POS_INFINITY) || buffer.equals(NEG_INFINITY)) {
  return convertInfinityOutput(buffer.equals(POS_INFINITY), type, targetClass);
 }
 TemporalAccessor parsed = context.getTimestampFormat().getParser().parse(buffer);
 ZonedDateTime dateTime;
 if (parsed.isSupported(ChronoField.OFFSET_SECONDS)) {
  dateTime = OffsetDateTime.from(parsed).toZonedDateTime().withZoneSameInstant(UTC_ID);
 }
 else {
  dateTime = LocalDateTime.from(parsed).atZone(calendar.getTimeZone().toZoneId());
 }
 return convertOutput(context, type, dateTime, targetClass, calendar);
}

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

private TemporalAccessor convertToJavaDate(Object value) throws OntopResultConversionException {
  TemporalAccessor dateValue = null;
  if (value instanceof Date ) {
    // If JDBC gives us proper Java object, we simply return the formatted version of the datatype
    Calendar calendar = DateUtils.toCalendar(((Date) value));
    TimeZone timeZone = calendar.getTimeZone();
    dateValue = OffsetDateTime.from(calendar.toInstant().atZone(timeZone.toZoneId()));
  } else {
    // Otherwise, we need to deal with possible String representation of datetime
    String stringValue = String.valueOf(value);
    for (DateTimeFormatter format : system2DateTimeFormatter.get(systemDB)) {
      try {
        dateValue = format.parseBest(stringValue, OffsetDateTime::from, LocalDateTime::from, LocalDate::from);
        break;
      } catch (DateTimeParseException e) {
        // continue with the next try
      }
    }
    if (dateValue == null) {
      throw new OntopResultConversionException("unparseable datetime: " + stringValue);
    }
  }
  return dateValue;
}

代码示例来源:origin: it.unibz.inf.ontop/ontop-system-sql-core

private TemporalAccessor convertToJavaDate(Object value) throws OntopResultConversionException {
  TemporalAccessor dateValue = null;
  if (value instanceof Date ) {
    // If JDBC gives us proper Java object, we simply return the formatted version of the datatype
    Calendar calendar = DateUtils.toCalendar(((Date) value));
    TimeZone timeZone = calendar.getTimeZone();
    dateValue = OffsetDateTime.from(calendar.toInstant().atZone(timeZone.toZoneId()));
  } else {
    // Otherwise, we need to deal with possible String representation of datetime
    String stringValue = String.valueOf(value);
    for (DateTimeFormatter format : system2DateTimeFormatter.get(systemDB)) {
      try {
        dateValue = format.parseBest(stringValue, OffsetDateTime::from, LocalDateTime::from, LocalDate::from);
        break;
      } catch (DateTimeParseException e) {
        // continue with the next try
      }
    }
    if (dateValue == null) {
      throw new OntopResultConversionException("unparseable datetime: " + stringValue);
    }
  }
  return dateValue;
}

代码示例来源:origin: com.impossibl.pgjdbc-ng/pgjdbc-ng

return OffsetDateTime.from(parsed);

相关文章