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

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

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

OffsetDateTime.toZonedDateTime介绍

[英]Converts this date-time to a ZonedDateTime using the offset as the zone ID.

This creates the simplest possible ZonedDateTime using the offset as the zone ID.

To control the time-zone used, see #atZoneSameInstant(ZoneId) and #atZoneSimilarLocal(ZoneId).
[中]使用偏移量作为分区ID,将此日期时间转换为ZonedDateTime。
这将使用偏移量作为分区ID创建最简单的分区DateTime。
要控制所使用的时区,请参见#atzonesamainstant(ZoneId)和#atzonesmilarLocal(ZoneId)。

代码示例

代码示例来源:origin: spring-projects/spring-framework

  1. @Override
  2. public ZonedDateTime convert(OffsetDateTime source) {
  3. return source.toZonedDateTime();
  4. }
  5. }

代码示例来源:origin: org.springframework/spring-context

  1. @Override
  2. public ZonedDateTime convert(OffsetDateTime source) {
  3. return source.toZonedDateTime();
  4. }
  5. }

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

  1. public static DateTimeValue datetime( OffsetDateTime datetime )
  2. {
  3. return new DateTimeValue( requireNonNull( datetime, "OffsetDateTime" ).toZonedDateTime() );
  4. }

代码示例来源:origin: spring-cloud/spring-cloud-gateway

  1. @Override
  2. public ZonedDateTime convert(String source) {
  3. ZonedDateTime dateTime;
  4. try {
  5. long epoch = Long.parseLong(source);
  6. dateTime = Instant.ofEpochMilli(epoch).atOffset(ZoneOffset.ofTotalSeconds(0))
  7. .toZonedDateTime();
  8. } catch (NumberFormatException e) {
  9. // try ZonedDateTime instead
  10. dateTime = ZonedDateTime.parse(source);
  11. }
  12. return dateTime;
  13. }
  14. }

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

  1. @Override
  2. @SuppressWarnings("unchecked")
  3. public <X> X unwrap(OffsetTime offsetTime, Class<X> type, WrapperOptions options) {
  4. if ( offsetTime == null ) {
  5. return null;
  6. }
  7. if ( OffsetTime.class.isAssignableFrom( type ) ) {
  8. return (X) offsetTime;
  9. }
  10. if ( java.sql.Time.class.isAssignableFrom( type ) ) {
  11. return (X) java.sql.Time.valueOf( offsetTime.toLocalTime() );
  12. }
  13. final ZonedDateTime zonedDateTime = offsetTime.atDate( LocalDate.of( 1970, 1, 1 ) ).toZonedDateTime();
  14. if ( Timestamp.class.isAssignableFrom( type ) ) {
  15. return (X) Timestamp.valueOf( zonedDateTime.toLocalDateTime() );
  16. }
  17. if ( Calendar.class.isAssignableFrom( type ) ) {
  18. return (X) GregorianCalendar.from( zonedDateTime );
  19. }
  20. final Instant instant = zonedDateTime.toInstant();
  21. if ( Long.class.isAssignableFrom( type ) ) {
  22. return (X) Long.valueOf( instant.toEpochMilli() );
  23. }
  24. if ( java.util.Date.class.isAssignableFrom( type ) ) {
  25. return (X) java.util.Date.from( instant );
  26. }
  27. throw unknownUnwrap( type );
  28. }

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

  1. @Override
  2. @SuppressWarnings("unchecked")
  3. public <X> X unwrap(OffsetDateTime offsetDateTime, Class<X> type, WrapperOptions options) {
  4. if ( offsetDateTime == null ) {
  5. return null;
  6. }
  7. if ( OffsetDateTime.class.isAssignableFrom( type ) ) {
  8. return (X) offsetDateTime;
  9. }
  10. if ( Calendar.class.isAssignableFrom( type ) ) {
  11. return (X) GregorianCalendar.from( offsetDateTime.toZonedDateTime() );
  12. }
  13. if ( Timestamp.class.isAssignableFrom( type ) ) {
  14. return (X) Timestamp.from( offsetDateTime.toInstant() );
  15. }
  16. if ( java.sql.Date.class.isAssignableFrom( type ) ) {
  17. return (X) java.sql.Date.from( offsetDateTime.toInstant() );
  18. }
  19. if ( java.sql.Time.class.isAssignableFrom( type ) ) {
  20. return (X) java.sql.Time.from( offsetDateTime.toInstant() );
  21. }
  22. if ( Date.class.isAssignableFrom( type ) ) {
  23. return (X) Date.from( offsetDateTime.toInstant() );
  24. }
  25. if ( Long.class.isAssignableFrom( type ) ) {
  26. return (X) Long.valueOf( offsetDateTime.toInstant().toEpochMilli() );
  27. }
  28. throw unknownUnwrap( type );
  29. }

代码示例来源:origin: apache/servicemix-bundles

  1. @Override
  2. public ZonedDateTime convert(OffsetDateTime source) {
  3. return source.toZonedDateTime();
  4. }
  5. }

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

  1. static final DateTimeFormatter DATE_TIME_OPTIONAL_OFFSET =
  2. DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss[xxx]");
  3. OffsetDateTime odt =
  4. DATE_TIME_OPTIONAL_OFFSET.withZone(ZoneOffset.UTC).parse(input, OffsetDateTime::from);
  5. ZonedDateTime zdt = odt.toZonedDateTime(); // containing a fixed offset

代码示例来源:origin: org.codehaus.groovy/groovy-datetime

  1. /**
  2. * Returns a generally equivalent instance of {@link java.util.Calendar}.
  3. * The time value of the returned calendar is truncated to milliseconds and the time zone
  4. * is based on the offset of this date/time.
  5. *
  6. * @param self an OffsetDateTime
  7. * @return a java.util.Calendar
  8. * @since 2.5.0
  9. */
  10. public static Calendar toCalendar(final OffsetDateTime self) {
  11. return toCalendar(self.toZonedDateTime());
  12. }

代码示例来源:origin: org.neo4j.driver/neo4j-java-driver

  1. public static Value value( OffsetDateTime offsetDateTime )
  2. {
  3. return new DateTimeValue( offsetDateTime.toZonedDateTime() );
  4. }

代码示例来源:origin: salesforce/grpc-java-contrib

  1. public static ZonedDateTime toZonedDateTimeUtc(@Nonnull Timestamp timestamp) {
  2. checkNotNull(timestamp, "timestamp");
  3. return toOffsetDateTimeUtc(timestamp).toZonedDateTime();
  4. }

代码示例来源:origin: com.salesforce.servicelibs/grpc-contrib

  1. public static ZonedDateTime toZonedDateTimeUtc(@Nonnull Timestamp timestamp) {
  2. checkNotNull(timestamp, "timestamp");
  3. return toOffsetDateTimeUtc(timestamp).toZonedDateTime();
  4. }

代码示例来源:origin: org.neo4j/neo4j-values

  1. public static DateTimeValue datetime( OffsetDateTime datetime )
  2. {
  3. return new DateTimeValue( requireNonNull( datetime, "OffsetDateTime" ).toZonedDateTime() );
  4. }

代码示例来源:origin: brettwooldridge/HikariJSON

  1. private Date parseDate(String stringHolder)
  2. {
  3. return dateCache.computeIfAbsent(stringHolder, (k) -> {
  4. final OffsetDateTime dateTime = OffsetDateTime.parse(stringHolder);
  5. return GregorianCalendar.from(dateTime.toZonedDateTime()).getTime();
  6. });
  7. }

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

  1. @Override
  2. public ZonedDateTime time(ZonedDateTime from) {
  3. if (from == null) {
  4. return null;
  5. }
  6. OffsetTime offsetTime = from.toOffsetDateTime().toOffsetTime();
  7. return offsetTime.atDate(LocalDate.MIN).toZonedDateTime();
  8. }

代码示例来源:origin: org.apache.james/james-server-data-jpa

  1. @Override
  2. public ZonedDateTime getActivationDateForActiveScript(User user) throws StorageException, ScriptNotFoundException {
  3. Optional<JPASieveScript> script = findActiveSieveScript(user);
  4. JPASieveScript activeSieveScript = script.orElseThrow(() -> new ScriptNotFoundException("Unable to find active script for user " + user.asString()));
  5. return activeSieveScript.getActivationDateTime().toZonedDateTime();
  6. }

代码示例来源:origin: goldmansachs/jdmn

  1. @Override
  2. public ZonedDateTime time(ZonedDateTime from) {
  3. if (from == null) {
  4. return null;
  5. }
  6. OffsetTime offsetTime = from.toOffsetDateTime().toOffsetTime();
  7. return offsetTime.atDate(LocalDate.MIN).toZonedDateTime();
  8. }

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

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

代码示例来源:origin: jneat/mybatis-types

  1. @Override
  2. public void setNonNullParameter(PreparedStatement ps, int i, OffsetDateTime parameter, JdbcType jdbcType) throws SQLException {
  3. // Postgres do not work with offsets > than 15 hours, maybe other DBs too
  4. int offset = parameter.get(ChronoField.OFFSET_SECONDS);
  5. if (Math.abs(offset) > 54000) {
  6. parameter = parameter.withOffsetSameInstant(ZoneOffset.ofHours(offset > 0 ? 15 : -15));
  7. }
  8. ps.setTimestamp(
  9. i,
  10. Timestamp.from(parameter.toInstant()),
  11. GregorianCalendar.from(parameter.toZonedDateTime())
  12. );
  13. }

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

  1. private ZonedDateTime makeDateTime(String literal) {
  2. if (StringUtils.isBlank(literal)) {
  3. return null;
  4. }
  5. try {
  6. literal = DateTimeUtil.fixDateTimeFormat(literal);
  7. if (DateTimeUtil.hasZone(literal)) {
  8. return makeZonedDateTime(literal);
  9. } else if (DateTimeUtil.hasOffset(literal)) {
  10. return OffsetDateTime.parse(literal).toZonedDateTime();
  11. } else {
  12. return makeZonedDateTime(literal + "Z");
  13. }
  14. } catch (Throwable e) {
  15. String message = String.format("makeDateTime(%s)", literal);
  16. logError(message, e);
  17. return null;
  18. }
  19. }

相关文章