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

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

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

ZonedDateTime.of介绍

[英]Obtains an instance of ZonedDateTime from a year, month, day, hour, minute, second, nanosecond and time-zone.

This creates a zoned date-time matching the local date-time of the seven specified fields as closely as possible. Time-zone rules, such as daylight savings, mean that not every local date-time is valid for the specified zone, thus the local date-time may be adjusted.

The local date-time is resolved to a single instant on the time-line. This is achieved by finding a valid offset from UTC/Greenwich for the local date-time as defined by the ZoneRules of the zone ID.

In most cases, there is only one valid offset for a local date-time. In the case of an overlap, when clocks are set back, there are two valid offsets. This method uses the earlier offset typically corresponding to "summer".

In the case of a gap, when clocks jump forward, there is no valid offset. Instead, the local date-time is adjusted to be later by the length of the gap. For a typical one hour daylight savings change, the local date-time will be moved one hour later into the offset typically corresponding to "summer".

This method exists primarily for writing test cases. Non test-code will typically use other methods to create an offset time. LocalDateTime has five additional convenience variants of the equivalent factory method taking fewer arguments. They are not provided here to reduce the footprint of the API.
[中]从年、月、日、小时、分钟、秒、纳秒和时区获取ZoneDateTime的实例。
这将创建与七个指定字段的本地日期时间尽可能接近的分区日期时间。时区规则(如夏令时)意味着并非每个本地日期时间都对指定的区域有效,因此可以调整本地日期时间。
本地日期时间解析为时间线上的一个瞬间。这是通过查找由分区ID的分区表定义的本地日期时间与UTC/格林威治的有效偏移量来实现的。
在大多数情况下,本地日期时间只有一个有效偏移量。在重叠的情况下,当时钟被向后设置时,有两个有效偏移。此方法使用通常与“summer”对应的较早偏移量。
在间隙的情况下,当时钟向前跳时,没有有效的偏移。相反,本地日期时间会根据间隔的长度调整为更晚。对于典型的一小时夏令时更改,本地日期时间将在一小时后移动到通常对应于“夏季”的偏移量中。
这种方法主要用于编写测试用例。非测试代码通常会使用其他方法来创建偏移时间。LocalDateTime有五个附加的方便变量,它们是等效工厂方法的变体,使用的参数更少。这里不提供它们是为了减少API的占用空间。

代码示例

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

@Override
public ZonedDateTime createdAt() {
  return ZonedDateTime.of(2016, 12, 16, 12, 35, 0, 0, ZoneOffset.UTC);
}

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

private static ZonedDateTime newZonedDateTime( long epochSecondLocal, long nano, ZoneId zoneId )
  {
    Instant instant = Instant.ofEpochSecond( epochSecondLocal, nano );
    LocalDateTime localDateTime = LocalDateTime.ofInstant( instant, UTC );
    return ZonedDateTime.of( localDateTime, zoneId );
  }
}

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

/**
 * Converts local date time to Calendar.
 */
public static Calendar toCalendar(final LocalDateTime localDateTime) {
  return GregorianCalendar.from(ZonedDateTime.of(localDateTime, ZoneId.systemDefault()));
}

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

public static DateTimeValue datetime(
    int year, int month, int day, int hour, int minute, int second, int nanoOfSecond, ZoneId zone )
{
  return new DateTimeValue( assertValidArgument(
      () -> ZonedDateTime.of( year, month, day, hour, minute, second, nanoOfSecond, zone ) ) );
}

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

@Test
public void shouldRangeSeekInOrderAscendingDateTimeArray() throws Exception
{
  Object o0 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 0, ZoneId.of( "UTC" ) )};
  Object o1 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 1, ZoneId.of( "UTC" ) )};
  Object o2 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 2, ZoneId.of( "UTC" ) )};
  Object o3 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 3, ZoneId.of( "UTC" ) )};
  Object o4 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 4, ZoneId.of( "UTC" ) )};
  Object o5 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 5, ZoneId.of( "UTC" ) )};
  shouldRangeSeekInOrder( IndexOrder.ASCENDING, o0, o1, o2, o3, o4, o5 );
}

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

@Test
public void shouldRangeSeekInOrderDescendingDateTimeArray() throws Exception
{
  Object o0 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 0, ZoneId.of( "UTC" ) )};
  Object o1 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 1, ZoneId.of( "UTC" ) )};
  Object o2 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 2, ZoneId.of( "UTC" ) )};
  Object o3 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 3, ZoneId.of( "UTC" ) )};
  Object o4 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 4, ZoneId.of( "UTC" ) )};
  Object o5 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 5, ZoneId.of( "UTC" ) )};
  shouldSeekInOrderExactWithRange( IndexOrder.DESCENDING, o0, o1, o2, o3, o4, o5 );
}

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

@Test
public void shouldRangeSeekInOrderDescendingDateTimeArray() throws Exception
{
  Object o0 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 0, ZoneId.of( "UTC" ) )};
  Object o1 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 1, ZoneId.of( "UTC" ) )};
  Object o2 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 2, ZoneId.of( "UTC" ) )};
  Object o3 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 3, ZoneId.of( "UTC" ) )};
  Object o4 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 4, ZoneId.of( "UTC" ) )};
  Object o5 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 5, ZoneId.of( "UTC" ) )};
  shouldRangeSeekInOrder( IndexOrder.DESCENDING, o0, o1, o2, o3, o4, o5 );
}

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

@Test
public void shouldRangeSeekInOrderAscendingDateTimeArray() throws Exception
{
  Object o0 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 0, ZoneId.of( "UTC" ) )};
  Object o1 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 1, ZoneId.of( "UTC" ) )};
  Object o2 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 2, ZoneId.of( "UTC" ) )};
  Object o3 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 3, ZoneId.of( "UTC" ) )};
  Object o4 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 4, ZoneId.of( "UTC" ) )};
  Object o5 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 5, ZoneId.of( "UTC" ) )};
  shouldSeekInOrderExactWithRange( IndexOrder.ASCENDING, o0, o1, o2, o3, o4, o5 );
}

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

public static DateTimeValue datetime( DateValue date, LocalTimeValue time, ZoneId zone )
{
  return new DateTimeValue( ZonedDateTime.of( date.temporal(), time.temporal(), zone ) );
}

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

@Test
public void lastModified() {
  HttpHeaders headers = new HttpHeaders();
  ZonedDateTime lastModified = ZonedDateTime.of(2018, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC"));
  headers.setLastModified(lastModified.toInstant().toEpochMilli());
  HeaderAssertions assertions = headerAssertions(headers);
  assertions.lastModified(lastModified.toInstant().toEpochMilli());
  try {
    assertions.lastModified(lastModified.toInstant().toEpochMilli() + 1);
    fail("Wrong value expected");
  }
  catch (AssertionError error) {
    // Expected
  }
}

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

private TemporalValue attachDate( TemporalValue temporal, LocalDate dateToAttach )
{
  LocalTime timePart = temporal.getLocalTimePart();
  if ( temporal.supportsTimeZone() )
  {
    // turn time into date time
    return datetime( ZonedDateTime.of( dateToAttach, timePart, temporal.getZoneOffset() ) );
  }
  else
  {
    // turn local time into local date time
    return localDateTime( LocalDateTime.of( dateToAttach, timePart ) );
  }
}

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

@Test
public void expires() {
  HttpHeaders headers = new HttpHeaders();
  ZonedDateTime expires = ZonedDateTime.of(2018, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC"));
  headers.setExpires(expires);
  HeaderAssertions assertions = headerAssertions(headers);
  assertions.expires(expires.toInstant().toEpochMilli());
  try {
    assertions.expires(expires.toInstant().toEpochMilli() + 1);
    fail("Wrong value expected");
  }
  catch (AssertionError error) {
    // Expected
  }
}

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

@Test // SPR-17330
public void matchDateFormattedWithHttpHeaders() throws Exception {
  long epochMilli = ZonedDateTime.of(2018, 10, 5, 0, 0, 0, 0, ZoneId.of("GMT")).toInstant().toEpochMilli();
  HttpHeaders headers = new HttpHeaders();
  headers.setDate("myDate", epochMilli);
  this.response.setHeader("d", headers.getFirst("myDate"));
  this.matchers.dateValue("d", epochMilli).match(this.mvcResult);
}

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

@Test
public void createDateTimeFormatterWithTimeZone() {
  factory.setPattern("yyyyMMddHHmmss Z");
  factory.setTimeZone(TEST_TIMEZONE);
  ZoneId dateTimeZone = TEST_TIMEZONE.toZoneId();
  ZonedDateTime dateTime = ZonedDateTime.of(2009, 10, 21, 12, 10, 00, 00, dateTimeZone);
  String offset = (TEST_TIMEZONE.equals(NEW_YORK) ? "-0400" : "+0200");
  assertThat(factory.createDateTimeFormatter().format(dateTime), is("20091021121000 " + offset));
}

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

@Test
public void expiresZonedDateTime() {
  ZonedDateTime zonedDateTime = ZonedDateTime.of(2008, 12, 18, 10, 20, 0, 0, ZoneId.of("GMT"));
  headers.setExpires(zonedDateTime);
  assertEquals("Invalid Expires header", zonedDateTime.toInstant().toEpochMilli(), headers.getExpires());
  assertEquals("Invalid Expires header", "Thu, 18 Dec 2008 10:20:00 GMT", headers.getFirst("expires"));
}

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

public static DateTimeValue datetime( DateValue date, TimeValue time )
{
  OffsetTime t = time.temporal();
  return new DateTimeValue( ZonedDateTime.of( date.temporal(), t.toLocalTime(), t.getOffset() ) );
}

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

@Override
  protected DateTimeValue selectDateTime( AnyValue datetime )
  {
    if ( datetime instanceof DateTimeValue )
    {
      DateTimeValue value = (DateTimeValue) datetime;
      ZoneId zone = optionalTimezone();
      return zone == null ? value : new DateTimeValue(
          ZonedDateTime.of( value.temporal().toLocalDateTime(), zone ) );
    }
    if ( datetime instanceof LocalDateTimeValue )
    {
      return new DateTimeValue( ZonedDateTime.of(
          ((LocalDateTimeValue) datetime).temporal(), timezone() ) );
    }
    throw new UnsupportedTemporalUnitException( "Cannot select datetime from: " + datetime );
  }
};

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

@Test
public void firstZonedDateTime() {
  ZonedDateTime date = ZonedDateTime.of(2017, 6, 22, 22, 22, 0, 0, ZoneId.of("GMT"));
  headers.setZonedDateTime(HttpHeaders.DATE, date);
  assertThat(headers.getFirst(HttpHeaders.DATE), is("Thu, 22 Jun 2017 22:22:00 GMT"));
  assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date));
  headers.clear();
  ZonedDateTime otherDate = ZonedDateTime.of(2010, 12, 18, 10, 20, 0, 0, ZoneId.of("GMT"));
  headers.add(HttpHeaders.DATE, RFC_1123_DATE_TIME.format(date));
  headers.add(HttpHeaders.DATE, RFC_1123_DATE_TIME.format(otherDate));
  assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date));
  // obsolete RFC 850 format
  headers.clear();
  headers.set(HttpHeaders.DATE, "Thursday, 22-Jun-17 22:22:00 GMT");
  assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date));
  // ANSI C's asctime() format
  headers.clear();
  headers.set(HttpHeaders.DATE, "Thu Jun 22 22:22:00 2017");
  assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date));
}

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

@Test
public void shouldPackLocalDateTimeWithTimeZoneId()
{
  LocalDateTime localDateTime = LocalDateTime.of( 1999, 12, 30, 9, 49, 20, 999999999 );
  ZoneId zoneId = ZoneId.of( "Europe/Stockholm" );
  ZonedDateTime zonedDateTime = ZonedDateTime.of( localDateTime, zoneId );
  PackedOutputArray packedOutput = pack( datetime( zonedDateTime ) );
  ByteBuffer buffer = ByteBuffer.wrap( packedOutput.bytes() );
  buffer.getShort(); // skip struct header
  assertEquals( INT_32, buffer.get() );
  assertEquals( localDateTime.toEpochSecond( UTC ), buffer.getInt() );
  assertEquals( INT_32, buffer.get() );
  assertEquals( localDateTime.getNano(), buffer.getInt() );
  buffer.getShort(); // skip zoneId string header
  byte[] zoneIdBytes = new byte[zoneId.getId().getBytes( UTF_8 ).length];
  buffer.get( zoneIdBytes );
  assertEquals( zoneId.getId(), new String( zoneIdBytes, UTF_8 ) );
}

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

@Test
public void shouldPackLocalDateTimeWithTimeZoneOffset()
{
  LocalDateTime localDateTime = LocalDateTime.of( 2015, 3, 23, 19, 15, 59, 10 );
  ZoneOffset offset = ZoneOffset.ofHoursMinutes( -5, -15 );
  ZonedDateTime zonedDateTime = ZonedDateTime.of( localDateTime, offset );
  PackedOutputArray packedOutput = pack( datetime( zonedDateTime ) );
  ByteBuffer buffer = ByteBuffer.wrap( packedOutput.bytes() );
  buffer.getShort(); // skip struct header
  assertEquals( INT_32, buffer.get() );
  assertEquals( localDateTime.toEpochSecond( UTC ), buffer.getInt() );
  assertEquals( localDateTime.getNano(), buffer.get() );
  assertEquals( INT_16, buffer.get() );
  assertEquals( offset.getTotalSeconds(), buffer.getShort() );
}

相关文章

ZonedDateTime类方法