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

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

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

ZonedDateTime.withZoneSameLocal介绍

[英]Returns a copy of this date-time with a different time-zone, retaining the local date-time if possible.

This method changes the time-zone and retains the local date-time. The local date-time is only changed if it is invalid for the new zone, determined using the same approach as #ofLocal(LocalDateTime,ZoneId,ZoneOffset).

To change the zone and adjust the local date-time, use #withZoneSameInstant(ZoneId).

This instance is immutable and unaffected by this method call.
[中]返回具有不同时区的此日期时间的副本,如果可能,保留本地日期时间。
此方法更改时区并保留本地日期时间。只有当本地日期时间对新区域无效时,才会更改它,使用与#ofLocal(LocalDateTime、ZoneId、ZoneOffset)相同的方法确定。
要更改区域并调整本地日期时间,请使用#withZoneSameInstant(ZoneId)。
此实例是不可变的,不受此方法调用的影响。

代码示例

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

public TimestampLocalTZWritable evaluate(TimestampLocalTZWritable t) {
 if (t == null) {
  return null;
 }
 final ZonedDateTime localZDT = t.getTimestampTZ().getZonedDateTime(); // default
 final long originalTimestampUTC = localZDT.withZoneSameLocal(ZoneOffset.UTC)
   .toInstant().toEpochMilli(); // default -> utc
 final long newTimestampUTC = granularity.truncate(originalTimestampUTC); // utc
 final ZonedDateTime newLocalZDT = ZonedDateTime.of(
   LocalDateTime.ofInstant(Instant.ofEpochMilli(newTimestampUTC), ZoneOffset.UTC),
   localZDT.getZone()); // utc -> default
 resultTSLTZ.set(new TimestampTZ(newLocalZDT));
 return resultTSLTZ;
}

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

public static SqlTime sqlTimeOf(LocalTime time, Session session)
{
  if (session.toConnectorSession().isLegacyTimestamp()) {
    long millisUtc = LocalDate.ofEpochDay(0)
        .atTime(time)
        .atZone(UTC)
        .withZoneSameLocal(ZoneId.of(session.getTimeZoneKey().getId()))
        .toInstant()
        .toEpochMilli();
    return new SqlTime(millisUtc, session.getTimeZoneKey());
  }
  return new SqlTime(NANOSECONDS.toMillis(time.toNanoOfDay()));
}

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

result = result.withZoneSameLocal( timezone() );

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

if ( timezone != NO_VALUE )
  truncatedZDT = truncatedZDT.withZoneSameLocal( timezoneOf( timezone ) );

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

public ZonedDateTime withZoneSameLocal(ZoneId zone) {
  return dt.withZoneSameLocal(zone);
}

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

@Test
@FrozenClockRule.TimeZone( {"Europe/Stockholm", "America/Los_Angeles"} )
public void shouldCopyDateTime()
{
  assertEqualTemporal(
      datetime( ZonedDateTime.now( clock ) ),
      builder( clock ).add( "datetime", datetime( ZonedDateTime.now( clock ) ) ).build() );
  assertEqualTemporal(
      datetime( ZonedDateTime.now( clock ) ),
      builder( clock )
          .add( "datetime", localDateTime( LocalDateTime.now( clock ) ) )
          .build() );
  assertEqualTemporal(
      datetime( ZonedDateTime.now( clock ).withZoneSameLocal( ZoneId.of( "America/New_York" ) ) ),
      builder( clock )
          .add( "datetime", localDateTime( LocalDateTime.now( clock ) ) )
          .add( "timezone", stringValue( "America/New_York" ) )
          .build() );
}

代码示例来源:origin: rakam-io/rakam

private static ZonedDateTime parse(String value) {
  return ZonedDateTime.parse(value).withZoneSameLocal(ZoneId.of("UTC"));
}

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

private long parseDateTime(String value, ZoneId timeZone, boolean roundUpIfNoTime) {
    DateFormatter formatter = roundUpIfNoTime ? this.roundUpFormatter : this.formatter;
    try {
      if (timeZone == null) {
        return DateFormatters.toZonedDateTime(formatter.parse(value)).toInstant().toEpochMilli();
      } else {
        TemporalAccessor accessor = formatter.parse(value);
        ZoneId zoneId = TemporalQueries.zone().queryFrom(accessor);
        if (zoneId != null) {
          timeZone = zoneId;
        }

        return DateFormatters.toZonedDateTime(accessor).withZoneSameLocal(timeZone).toInstant().toEpochMilli();
      }
    } catch (IllegalArgumentException | DateTimeException e) {
      throw new ElasticsearchParseException("failed to parse date field [{}]: [{}]", e, value, e.getMessage());
    }
  }
}

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

ZonedDateTime zdtMontreal = 
  ZonedDateTime.now( ZoneId.of( "America/Montreal" ) );

ZonedDateTime zdtAuckland = 
  zdtMontreal.withZoneSameLocal( ZoneId.of( "Pacific/Auckland" ) );

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

public ZonedDateTime withZoneSameLocal(ZoneId zone) {
  return dt.withZoneSameLocal(zone);
}

代码示例来源:origin: com.guestful.module/guestful.module.jsr310-extensions

public static ZonedDateTime inParis(ZonedDateTime dt) {
  return dt.withZoneSameLocal(ZoneId.of("Europe/Paris"));
}

代码示例来源:origin: com.guestful.module/guestful.module.jsr310-extensions

public static ZonedDateTime inMontreal(ZonedDateTime dt) {
  return dt.withZoneSameLocal(ZoneId.of("America/Montreal"));
}

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

ZonedDateTime dateTime = ZonedDateTime.parse("2016-10-20T11:34:57+02:00[Europe/Zurich]"); // UTC + 2
long millisEurope = dateTime.toInstant().toEpochMilli();
System.out.println(millisEurope); // 1476956097000

dateTime = dateTime.withZoneSameLocal(ZoneId.of("America/Los_Angeles")); // UTC - 7
long millisAmerica = dateTime.toInstant().toEpochMilli();
System.out.println(millisAmerica); // 1476988497000

// The difference between UTC + 2 and UTC - 7  == -9
System.out.println((millisEurope - millisAmerica) / 1000 / 60 / 60);

代码示例来源:origin: owlike/genson

@Override
protected ZonedDateTime readFieldFromObject(ZonedDateTime obj, ObjectReader reader) {
  if(reader.name().equals(ZONE_ID_FIELD_NAME)){
    String zoneIdValue = reader.valueAsString();
    ZoneId zoneId = ZoneId.of(zoneIdValue);
    return obj.withZoneSameLocal(zoneId);
  }
  else {
    return super.readFieldFromObject(obj, reader);
  }
}

代码示例来源:origin: owlike/genson

@Override
public ZonedDateTime readFieldsFromArray(Supplier<ZonedDateTime> instanceProvider, ObjectReader reader) {
  ZonedDateTime zt = super.readFieldsFromArray(instanceProvider, reader);
  reader.next();
  String zoneId = reader.valueAsString();
  return zt.withZoneSameLocal(ZoneId.of(zoneId));
}

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

public static SqlTime sqlTimeOf(LocalTime time, Session session)
{
  if (session.toConnectorSession().isLegacyTimestamp()) {
    long millisUtc = LocalDate.ofEpochDay(0)
        .atTime(time)
        .atZone(UTC)
        .withZoneSameLocal(ZoneId.of(session.getTimeZoneKey().getId()))
        .toInstant()
        .toEpochMilli();
    return new SqlTime(millisUtc, session.getTimeZoneKey());
  }
  return new SqlTime(NANOSECONDS.toMillis(time.toNanoOfDay()));
}

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

public static SqlTime sqlTimeOf(LocalTime time, Session session)
{
  if (session.toConnectorSession().isLegacyTimestamp()) {
    long millisUtc = LocalDate.ofEpochDay(0)
        .atTime(time)
        .atZone(UTC)
        .withZoneSameLocal(ZoneId.of(session.getTimeZoneKey().getId()))
        .toInstant()
        .toEpochMilli();
    return new SqlTime(millisUtc, session.getTimeZoneKey());
  }
  return new SqlTime(NANOSECONDS.toMillis(time.toNanoOfDay()));
}

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

result = result.withZoneSameLocal(ZoneOffset.ofTotalSeconds(accessor.get(ChronoField.OFFSET_SECONDS)));

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

@DataProvider(name = "date")
  public synchronized Object[][] dateData() {
    if (dateCache == null) {
      dateCache = new Object[][]{
        new Object[]{20L, null, null, null},
        new Object[]{21L, LocalDateTime.now(), OffsetDateTime.now(), ZonedDateTime.now()},
        new Object[]{22L, LocalDateTime.now(), OffsetDateTime.now().withOffsetSameInstant(ZoneOffset.UTC), ZonedDateTime.now().withZoneSameInstant(ZoneId.of("Z"))},
        new Object[]{23L, LocalDateTime.now(), OffsetDateTime.now().withOffsetSameInstant(ZoneOffset.of("+14:00")), ZonedDateTime.now().withZoneSameInstant(ZoneId.of("Z"))},
        new Object[]{24L, LocalDateTime.now(), OffsetDateTime.now().withOffsetSameLocal(ZoneOffset.MAX), ZonedDateTime.now().withZoneSameLocal(ZoneId.of("GMT+13"))},
        new Object[]{25L, LocalDateTime.now(), OffsetDateTime.now().withOffsetSameInstant(ZoneOffset.MIN), ZonedDateTime.now().withZoneSameInstant(ZoneId.of("GMT-12"))}
      };
    }
    return dateCache;
  }
}

代码示例来源:origin: commercetools/commercetools-jvm-sdk

@Test
public void addPriceWithValidityPeriod() throws Exception {
  final PriceDraft expectedPrice = PriceDraft.of(MoneyImpl.of(123, EUR))
      .withValidFrom(SphereTestUtils.now())
      .withValidUntil(SphereTestUtils.now().withZoneSameLocal(ZoneOffset.UTC).plusHours(2));
  testAddPrice(expectedPrice);
}

相关文章

ZonedDateTime类方法