gpt4 book ai didi

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

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

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

ZoneId.of介绍

[英]Obtains an instance of ZoneId from an ID ensuring that the ID is valid and available for use.

This method parses the ID producing a ZoneId or ZoneOffset. A ZoneOffset is returned if the ID is 'Z', or starts with '+' or '-'. The result will always be a valid ID for which ZoneRules can be obtained.

Parsing matches the zone ID step by step as follows.

  • If the zone ID equals 'Z', the result is ZoneOffset.UTC.
  • If the zone ID consists of a single letter, the zone ID is invalid and DateTimeException is thrown.
  • If the zone ID starts with '+' or '-', the ID is parsed as a ZoneOffset using ZoneOffset#of(String).
  • If the zone ID equals 'GMT', 'UTC' or 'UT' then the result is a ZoneIdwith the same ID and rules equivalent to ZoneOffset.UTC.
  • If the zone ID starts with 'UTC+', 'UTC-', 'GMT+', 'GMT-', 'UT+' or 'UT-' then the ID is a prefixed offset-based ID. The ID is split in two, with a two or three letter prefix and a suffix starting with the sign. The suffix is parsed as a ZoneOffset#of(String). The result will be a ZoneId with the specified UTC/GMT/UT prefix and the normalized offset ID as per ZoneOffset#getId(). The rules of the returned ZoneId will be equivalent to the parsed ZoneOffset.
  • All other IDs are parsed as region-based zone IDs. Region IDs must match the regular expression [A-Za-z][A-Za-z0-9~/._+-]+ otherwise a DateTimeException is thrown. If the zone ID is not in the configured set of IDs, ZoneRulesException is thrown. The detailed format of the region ID depends on the group supplying the data. The default set of data is supplied by the IANA Time Zone Database (TZDB). This has region IDs of the form '{area}/{city}', such as 'Europe/Paris' or 'America/New_York'. This is compatible with most IDs from java.util.TimeZone.
    [中]从ID获取ZoneId的实例,确保该ID有效且可供使用。
    此方法解析产生ZoneId或ZoneOffset的ID。如果ID为“Z”,或以“+”或“-”开头,则返回ZoneOffset。结果将始终是可以获得ZoneRules的有效ID。
    解析按如下步骤逐步匹配区域ID。
    *如果分区ID等于“Z”,则结果为ZoneOffset。UTC。
    *如果区域ID由单个字母组成,则区域ID无效,并引发DateTimeException。
    *如果区域ID以“+”或“-”开头,则使用ZoneOffset#of(String)将ID解析为ZoneOffset。
    *如果区域ID等于“GMT”、“UTC”或“UT”,则结果是一个具有相同ID和与ZoneOffset等效的规则的区域ID。UTC。
    *如果区域ID以“UTC+”、“UTC-”、“GMT+”、“GMT-”、“UT+”或“UT-”开头,则该ID是基于偏移量的前缀ID。该ID被一分为二,前缀为两个或三个字母,后缀以符号开头。后缀被解析为(字符串)的区域偏移。结果将是一个带有指定UTC/GMT/UT前缀和根据ZoneOffset#getId()的标准化偏移ID的区域ID。返回的ZoneId的规则将等同于解析的ZoneOffset。
    *所有其他ID都被解析为基于区域的区域ID。区域ID必须与正则表达式[A-Za-z][A-Za-z0-9~/._+-]+匹配,否则会引发DateTimeException。如果区域ID不在已配置的ID集中,则会引发ZoneRulesException。区域ID的详细格式取决于提供数据的组。默认数据集由IANA时区数据库(TZDB)提供。它具有格式为“{area}/{city}”的区域ID,例如“Europe/Paris”或“America/New_York”。这与大多数来自java的ID兼容。util。时区。

代码示例

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

@Override
public void setAsText(String text) throws IllegalArgumentException {
  setValue(ZoneId.of(text));
}

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

@Override
  protected Object deserialize(String key, DeserializationContext ctxt) throws IOException {
    try {
      return ZoneId.of(key);
    } catch (DateTimeException e) {
      return _handleDateTimeException(ctxt, ZoneId.class, e, key);
    }
  }
}

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

@JsonValue
  @Override
  public String toString()
  {
    return Instant.ofEpochMilli(millisUtc).atZone(ZoneId.of(timeZoneKey.getId())).format(formatter);
  }
}

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

@JsonValue
@Override
public String toString()
{
  if (isLegacyTimestamp()) {
    return Instant.ofEpochMilli(millis).atZone(ZoneId.of(sessionTimeZoneKey.get().getId())).format(JSON_FORMATTER);
  }
  else {
    return Instant.ofEpochMilli(millis).atZone(ZoneId.of(UTC_KEY.getId())).format(JSON_FORMATTER);
  }
}

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

@JsonValue
@Override
public String toString()
{
  if (isLegacyTimestamp()) {
    return Instant.ofEpochMilli(millis).atZone(ZoneId.of(sessionTimeZoneKey.get().getId())).format(formatter);
  }
  else {
    return Instant.ofEpochMilli(millis).atZone(ZoneOffset.UTC).format(formatter);
  }
}

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

@JsonValue
@Override
public String toString()
{
  if (isLegacyTimestamp()) {
    return Instant.ofEpochMilli(millis).atZone(ZoneId.of(sessionTimeZoneKey.get().getId())).format(JSON_FORMATTER);
  }
  else {
    return Instant.ofEpochMilli(millis).atZone(ZoneId.of(UTC_KEY.getId())).format(JSON_FORMATTER);
  }
}

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

@JsonValue
  @Override
  public String toString()
  {
    return Instant.ofEpochMilli(millisUtc).atZone(ZoneId.of(timeZoneKey.getId())).format(formatter);
  }
}

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

@JsonValue
@Override
public String toString()
{
  if (isLegacyTimestamp()) {
    return Instant.ofEpochMilli(millis).atZone(ZoneId.of(sessionTimeZoneKey.get().getId())).format(formatter);
  }
  else {
    return Instant.ofEpochMilli(millis).atZone(ZoneOffset.UTC).format(formatter);
  }
}

代码示例来源: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: spring-projects/spring-framework

@Test
public void convertObjectToStringWithJavaTimeOfMethodPresent() {
  assertTrue(conversionService.convert(ZoneId.of("GMT+1"), String.class).startsWith("GMT+"));
}

代码示例来源: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 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: spring-projects/spring-framework

@Test
public void convertObjectToObjectWithJavaTimeOfMethod() {
  assertEquals(ZoneId.of("GMT+1"), conversionService.convert("GMT+1", ZoneId.class));
}

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

@Test
public void getValueAsText() {
  editor.setValue(ZoneId.of("America/New_York"));
  assertEquals("The text version is not correct.", "America/New_York", editor.getAsText());
}

代码示例来源: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: spring-projects/spring-framework

@Test
public void resolveCustomizedAndTimeZoneLocale() {
  TimeZone timeZone = TimeZone.getTimeZone(ZoneId.of("UTC"));
  FixedLocaleContextResolver resolver = new FixedLocaleContextResolver(FRANCE, timeZone);
  TimeZoneAwareLocaleContext context = (TimeZoneAwareLocaleContext) resolver.resolveLocaleContext(exchange());
  assertEquals(FRANCE, context.getLocale());
  assertEquals(timeZone, context.getTimeZone());
}

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

@Test
public void americaLosAngeles() {
  editor.setAsText("America/Los_Angeles");
  ZoneId zoneId = (ZoneId) editor.getValue();
  assertNotNull("The zone ID should not be null.", zoneId);
  assertEquals("The zone ID is not correct.", ZoneId.of("America/Los_Angeles"), zoneId);
  assertEquals("The text version is not correct.", "America/Los_Angeles", editor.getAsText());
}

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

@Test
public void americaChicago() {
  editor.setAsText("America/Chicago");
  ZoneId zoneId = (ZoneId) editor.getValue();
  assertNotNull("The zone ID should not be null.", zoneId);
  assertEquals("The zone ID is not correct.", ZoneId.of("America/Chicago"), zoneId);
  assertEquals("The text version is not correct.", "America/Chicago", editor.getAsText());
}

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

private void setup(DateTimeFormatterRegistrar registrar) {
  conversionService = new FormattingConversionService();
  DefaultConversionService.addDefaultConverters(conversionService);
  registrar.registerFormatters(conversionService);
  DateTimeBean bean = new DateTimeBean();
  bean.getChildren().add(new DateTimeBean());
  binder = new DataBinder(bean);
  binder.setConversionService(conversionService);
  LocaleContextHolder.setLocale(Locale.US);
  DateTimeContext context = new DateTimeContext();
  context.setTimeZone(ZoneId.of("-05:00"));
  DateTimeContextHolder.setDateTimeContext(context);
}

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