gpt4 book ai didi

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

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

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

ZoneId.getRules介绍

[英]Gets the time-zone rules for this ID allowing calculations to be performed.

The rules provide the functionality associated with a time-zone, such as finding the offset for a given instant or local date-time.

A time-zone can be invalid if it is deserialized in a Java Runtime which does not have the same rules loaded as the Java Runtime that stored it. In this case, calling this method will throw a ZoneRulesException.

The rules are supplied by ZoneRulesProvider. An advanced provider may support dynamic updates to the rules without restarting the Java Runtime. If so, then the result of this method may change over time. Each individual call will be still remain thread-safe.

ZoneOffset will always return a set of rules where the offset never changes.
[中]获取允许执行计算的此ID的时区规则。
这些规则提供与时区相关的功能,例如查找给定即时或本地日期时间的偏移量。
如果时区在Java运行时中被反序列化,而Java运行时没有加载与存储时区的Java运行时相同的规则,则时区可能无效。在这种情况下,调用此方法将引发ZoneRulesException。
这些规则由ZoneRulesProvider提供。高级提供者可以支持规则的动态更新,而无需重新启动Java运行时。如果是这样,那么这种方法的结果可能会随着时间的推移而改变。每个单独的调用仍将保持线程安全。
ZoneOffset将始终返回一组规则,其中偏移量永远不会更改。

代码示例

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

private static ZoneOffset parseOffset( Matcher matcher, Supplier<ZoneId> defaultZone )
{
  ZoneOffset offset = parseOffset( matcher );
  if ( offset == null )
  {
    ZoneId zoneId = defaultZone.get();
    offset = zoneId instanceof ZoneOffset ? (ZoneOffset) zoneId : zoneId.getRules().getOffset( Instant.now() );
  }
  return offset;
}

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

@Override
public Atop create(AtopTable table, ZonedDateTime date)
{
  checkArgument(date.getZone().getRules().equals(timeZone.getRules()), "Split date (%s) is not in the local timezone (%s)", date.getZone(), timeZone);
  ProcessBuilder processBuilder = new ProcessBuilder(executablePath);
  processBuilder.command().add("-P");
  processBuilder.command().add(table.getAtopLabel());
  processBuilder.command().add("-r");
  processBuilder.command().add(DATE_FORMATTER.format(date));
  Process process;
  try {
    process = processBuilder.start();
  }
  catch (IOException e) {
    throw new PrestoException(ATOP_CANNOT_START_PROCESS_ERROR, format("Cannot start %s", processBuilder.command()), e);
  }
  return new AtopProcess(process, readTimeout, executor);
}

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

final LocalDateTime localDateTime = (LocalDateTime) value;
  final ZoneId defaultZoneId = ZoneId.systemDefault();
  final ZoneOffset offset = defaultZoneId.getRules().getOffset(localDateTime);
  date = Date.from(localDateTime.toInstant(offset));
} else if (value instanceof LocalDate) {
  final LocalDateTime localDateTime = localDate.atStartOfDay();
  final ZoneId defaultZoneId = ZoneId.systemDefault();
  final ZoneOffset offset = defaultZoneId.getRules().getOffset(localDateTime);
  date = Date.from(localDateTime.toInstant(offset));
} else if (value instanceof Instant) {

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

@Test
  public void testDateToTimestampCoercion()
  {
    // allow running tests with a connector that supports TIMESTAMP but not DATE

    // ordinary date
    MaterializedResult rows = h2QueryRunner.execute(TEST_SESSION, "SELECT DATE '2018-01-13'", ImmutableList.of(TIMESTAMP));
    assertEquals(rows.getOnlyValue(), LocalDate.of(2018, 1, 13).atStartOfDay());

    // date, which midnight was skipped in JVM zone
    LocalDate forwardOffsetChangeAtMidnightInJvmZone = LocalDate.of(1970, 1, 1);
    checkState(ZoneId.systemDefault().getRules().getValidOffsets(forwardOffsetChangeAtMidnightInJvmZone.atStartOfDay()).size() == 0, "This test assumes certain JVM time zone");
    rows = h2QueryRunner.execute(TEST_SESSION, DateTimeFormatter.ofPattern("'SELECT DATE '''uuuu-MM-dd''").format(forwardOffsetChangeAtMidnightInJvmZone), ImmutableList.of(TIMESTAMP));
    assertEquals(rows.getOnlyValue(), forwardOffsetChangeAtMidnightInJvmZone.atStartOfDay());
  }
}

代码示例来源:origin: yu199195/Raincat

/**
 * 将当前时区时间转成UTC时间.
 *
 * @param dateTime 时间
 * @return LocalDateTime
 */
public static LocalDateTime toUTCDateTime(final LocalDateTime dateTime) {
  if (dateTime == null) {
    return null;
  } else {
    Instant instant = dateTime.toInstant(DEFAULT_ZONE.getRules().getOffset(dateTime));
    return LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), ZoneOffset.UTC);
  }
}

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

@Test
  public void testLocallyUnrepresentableTimeLiterals()
  {
    LocalDateTime localTimeThatDidNotExist = LocalDateTime.of(2017, 4, 2, 2, 10);
    checkState(ZoneId.systemDefault().getRules().getValidOffsets(localTimeThatDidNotExist).isEmpty(), "This test assumes certain JVM time zone");
    // This tests that both Presto runner and H2 can return TIMESTAMP value that never happened in JVM's zone (e.g. is not representable using java.sql.Timestamp)
    @Language("SQL") String sql = DateTimeFormatter.ofPattern("'SELECT TIMESTAMP '''uuuu-MM-dd HH:mm:ss''").format(localTimeThatDidNotExist);
    assertEquals(computeScalar(sql), localTimeThatDidNotExist); // this tests Presto and the QueryRunner
    assertQuery(sql); // this tests H2QueryRunner

    LocalDate localDateThatDidNotHaveMidnight = LocalDate.of(1970, 1, 1);
    checkState(ZoneId.systemDefault().getRules().getValidOffsets(localDateThatDidNotHaveMidnight.atStartOfDay()).isEmpty(), "This test assumes certain JVM time zone");
    // This tests that both Presto runner and H2 can return DATE value for a day which midnight never happened in JVM's zone (e.g. is not exactly representable using java.sql.Date)
    sql = DateTimeFormatter.ofPattern("'SELECT DATE '''uuuu-MM-dd''").format(localDateThatDidNotHaveMidnight);
    assertEquals(computeScalar(sql), localDateThatDidNotHaveMidnight); // this tests Presto and the QueryRunner
    assertQuery(sql); // this tests H2QueryRunner

    LocalTime localTimeThatDidNotOccurOn19700101 = LocalTime.of(0, 10);
    checkState(ZoneId.systemDefault().getRules().getValidOffsets(localTimeThatDidNotOccurOn19700101.atDate(LocalDate.ofEpochDay(0))).isEmpty(), "This test assumes certain JVM time zone");
    checkState(!Objects.equals(java.sql.Time.valueOf(localTimeThatDidNotOccurOn19700101).toLocalTime(), localTimeThatDidNotOccurOn19700101), "This test assumes certain JVM time zone");
    sql = DateTimeFormatter.ofPattern("'SELECT TIME '''HH:mm:ss''").format(localTimeThatDidNotOccurOn19700101);
    assertEquals(computeScalar(sql), localTimeThatDidNotOccurOn19700101); // this tests Presto and the QueryRunner
    assertQuery(sql); // this tests H2QueryRunner
  }
}

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

try
  expected = zone.getRules().getOffset( local );

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

@Override
public Date unmarshal(String date) throws Exception {
  final LocalDateTime now = LocalDateTime.now();
  final DateTimeFormatter parser = new DateTimeFormatterBuilder().appendPattern(DEFAULT_TIME_FORMAT)
      .parseDefaulting(ChronoField.YEAR, now.getYear())
      .parseDefaulting(ChronoField.MONTH_OF_YEAR, now.getMonthValue())
      .parseDefaulting(ChronoField.DAY_OF_MONTH, now.getDayOfMonth())
      .parseDefaulting(ChronoField.MILLI_OF_SECOND, 0)
      .toFormatter(Locale.US);
  final LocalDateTime parsedDateTime = LocalDateTime.parse(date, parser);
  return Date.from(parsedDateTime.toInstant(ZONE_ID.getRules().getOffset(now)));
}

代码示例来源:origin: stanfordnlp/CoreNLP

datetime.get(ChronoField.DAY_OF_MONTH)
).atStartOfDay().toInstant(ZoneOffset.UTC);
ZoneOffset currentOffsetForMyZone = timezone.get().getRules().getOffset(reference);
try {
 return Optional.of(java.time.LocalDateTime.of(

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

@Override
public Date unmarshal(String date) throws Exception {
  final LocalDateTime now = LocalDateTime.now();
  final DateTimeFormatter parser = new DateTimeFormatterBuilder().appendPattern(DEFAULT_DATE_TIME_FORMAT)
      .parseDefaulting(ChronoField.YEAR, now.getYear())
      .parseDefaulting(ChronoField.MONTH_OF_YEAR, now.getMonthValue())
      .parseDefaulting(ChronoField.DAY_OF_MONTH, now.getDayOfMonth())
      .parseDefaulting(ChronoField.HOUR_OF_DAY, now.getHour())
      .parseDefaulting(ChronoField.MINUTE_OF_HOUR, now.getMinute())
      .parseDefaulting(ChronoField.SECOND_OF_MINUTE, now.getSecond())
      .parseDefaulting(ChronoField.MILLI_OF_SECOND, 0)
      .toFormatter(Locale.US);
  final LocalDateTime parsedDateTime = LocalDateTime.parse(date, parser);
  return Date.from(parsedDateTime.toInstant(ZONE_ID.getRules().getOffset(now)));
}

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

@Test
public void testDate()
{
  // Note: there is identical test for MySQL
  ZoneId jvmZone = ZoneId.systemDefault();
  checkState(jvmZone.getId().equals("America/Bahia_Banderas"), "This test assumes certain JVM time zone");
  LocalDate dateOfLocalTimeChangeForwardAtMidnightInJvmZone = LocalDate.of(1970, 1, 1);
  verify(jvmZone.getRules().getValidOffsets(dateOfLocalTimeChangeForwardAtMidnightInJvmZone.atStartOfDay()).isEmpty());
  ZoneId someZone = ZoneId.of("Europe/Vilnius");
  LocalDate dateOfLocalTimeChangeForwardAtMidnightInSomeZone = LocalDate.of(1983, 4, 1);
  verify(someZone.getRules().getValidOffsets(dateOfLocalTimeChangeForwardAtMidnightInSomeZone.atStartOfDay()).isEmpty());
  LocalDate dateOfLocalTimeChangeBackwardAtMidnightInSomeZone = LocalDate.of(1983, 10, 1);
  verify(someZone.getRules().getValidOffsets(dateOfLocalTimeChangeBackwardAtMidnightInSomeZone.atStartOfDay().minusMinutes(1)).size() == 2);
  DataTypeTest testCases = DataTypeTest.create()
      .addRoundTrip(dateDataType(), LocalDate.of(1952, 4, 3)) // before epoch
      .addRoundTrip(dateDataType(), LocalDate.of(1970, 1, 1))
      .addRoundTrip(dateDataType(), LocalDate.of(1970, 2, 3))
      .addRoundTrip(dateDataType(), LocalDate.of(2017, 7, 1)) // summer on northern hemisphere (possible DST)
      .addRoundTrip(dateDataType(), LocalDate.of(2017, 1, 1)) // winter on northern hemisphere (possible DST on southern hemisphere)
      .addRoundTrip(dateDataType(), dateOfLocalTimeChangeForwardAtMidnightInJvmZone)
      .addRoundTrip(dateDataType(), dateOfLocalTimeChangeForwardAtMidnightInSomeZone)
      .addRoundTrip(dateDataType(), dateOfLocalTimeChangeBackwardAtMidnightInSomeZone);
  for (String timeZoneId : ImmutableList.of(UTC_KEY.getId(), jvmZone.getId(), someZone.getId())) {
    Session session = Session.builder(getQueryRunner().getDefaultSession())
        .setTimeZoneKey(TimeZoneKey.getTimeZoneKey(timeZoneId))
        .build();
    testCases.execute(getQueryRunner(), session, postgresCreateAndInsert("tpch.test_date"));
    testCases.execute(getQueryRunner(), session, prestoCreateAsSelect("test_date"));
  }
}

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

@Test
public void testDate()
{
  // Note: there is identical test for PostgreSQL
  ZoneId jvmZone = ZoneId.systemDefault();
  checkState(jvmZone.getId().equals("America/Bahia_Banderas"), "This test assumes certain JVM time zone");
  LocalDate dateOfLocalTimeChangeForwardAtMidnightInJvmZone = LocalDate.of(1970, 1, 1);
  verify(jvmZone.getRules().getValidOffsets(dateOfLocalTimeChangeForwardAtMidnightInJvmZone.atStartOfDay()).isEmpty());
  ZoneId someZone = ZoneId.of("Europe/Vilnius");
  LocalDate dateOfLocalTimeChangeForwardAtMidnightInSomeZone = LocalDate.of(1983, 4, 1);
  verify(someZone.getRules().getValidOffsets(dateOfLocalTimeChangeForwardAtMidnightInSomeZone.atStartOfDay()).isEmpty());
  LocalDate dateOfLocalTimeChangeBackwardAtMidnightInSomeZone = LocalDate.of(1983, 10, 1);
  verify(someZone.getRules().getValidOffsets(dateOfLocalTimeChangeBackwardAtMidnightInSomeZone.atStartOfDay().minusMinutes(1)).size() == 2);
  DataTypeTest testCases = DataTypeTest.create()
      .addRoundTrip(dateDataType(), LocalDate.of(1952, 4, 3)) // before epoch
      .addRoundTrip(dateDataType(), LocalDate.of(1970, 1, 1))
      .addRoundTrip(dateDataType(), LocalDate.of(1970, 2, 3))
      .addRoundTrip(dateDataType(), LocalDate.of(2017, 7, 1)) // summer on northern hemisphere (possible DST)
      .addRoundTrip(dateDataType(), LocalDate.of(2017, 1, 1)) // winter on northern hemisphere (possible DST on southern hemisphere)
      .addRoundTrip(dateDataType(), dateOfLocalTimeChangeForwardAtMidnightInJvmZone)
      .addRoundTrip(dateDataType(), dateOfLocalTimeChangeForwardAtMidnightInSomeZone)
      .addRoundTrip(dateDataType(), dateOfLocalTimeChangeBackwardAtMidnightInSomeZone);
  for (String timeZoneId : ImmutableList.of(UTC_KEY.getId(), jvmZone.getId(), someZone.getId())) {
    Session session = Session.builder(getQueryRunner().getDefaultSession())
        .setTimeZoneKey(TimeZoneKey.getTimeZoneKey(timeZoneId))
        .build();
    testCases.execute(getQueryRunner(), session, mysqlCreateAndInsert("tpch.test_date"));
    testCases.execute(getQueryRunner(), session, prestoCreateAsSelect("test_date"));
  }
}

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

private void assertTimestamp(String c4) {
  // '2014-09-08 17:51:04.777'
  // MySQL container is in UTC and the test time is during summer time period
  ZonedDateTime expectedTimestamp = ZonedDateTime.ofInstant(
      LocalDateTime.parse("2014-09-08T17:51:04.780").atZone(ZoneId.of("US/Samoa")).toInstant(),
      ZoneId.systemDefault());
  ZoneId defaultZoneId = ZoneId.systemDefault();
  ZonedDateTime c4DateTime = ZonedDateTime.parse(c4, ZonedTimestamp.FORMATTER).withZoneSameInstant(defaultZoneId);
  assertThat(c4DateTime.getYear()).isEqualTo(expectedTimestamp.getYear());
  assertThat(c4DateTime.getMonth()).isEqualTo(expectedTimestamp.getMonth());
  assertThat(c4DateTime.getDayOfMonth()).isEqualTo(expectedTimestamp.getDayOfMonth());
  assertThat(c4DateTime.getHour()).isEqualTo(expectedTimestamp.getHour());
  assertThat(c4DateTime.getMinute()).isEqualTo(expectedTimestamp.getMinute());
  assertThat(c4DateTime.getSecond()).isEqualTo(expectedTimestamp.getSecond());
  assertThat(c4DateTime.getNano()).isEqualTo(expectedTimestamp.getNano());
  // We're running the connector in the same timezone as the server, so the timezone in the timestamp
  // should match our current offset ...
  LocalDateTime expectedLocalDateTime = LocalDateTime.parse("2014-09-08T17:51:04.780");
  ZoneOffset expectedOffset = defaultZoneId.getRules().getOffset(expectedLocalDateTime);
  assertThat(c4DateTime.getOffset()).isEqualTo(expectedOffset);
}

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

ZoneId zoneId = ZoneId.of("Australia/Sydney");
ZoneRules rules = zoneId.getRules();

ZoneOffsetTransition nextTransition = rules.nextTransition(Instant.now());
System.out.println("Next transition at: " +
    nextTransition.getInstant().atZone(zoneId));

ZoneOffsetTransition nextNextTransition =
    rules.nextTransition(nextTransition.getInstant());
System.out.println("Next transition after that at: " +
    nextNextTransition.getInstant().atZone(zoneId));

代码示例来源:origin: com.vaadin/vaadin-server

return null;
ZoneRules rules = zoneId.getRules();
TimeZone timeZone = TimeZone.getTimeZone(zoneId);
List<Long> transitionsList = new ArrayList<>();

代码示例来源:origin: io.airlift/joda-to-java-time-bridge

JdkBasedDateTimeZone(String zoneId)
{
  super(zoneId);
  this.zoneRules = ZoneId.of(zoneId).getRules();
}

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

ZoneId zone = ZoneId.of("CET");
System.out.println(zone);
System.out.println(zone.getRules());
for (ZoneOffsetTransition trans : zone.getRules().getTransitions()) {
 System.out.println(trans);
}
for (ZoneOffsetTransitionRule rule : zone.getRules().getTransitionRules()) {
 System.out.println(rule);
}

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

/**
 * Returns a {@link java.time.ZoneOffset} for this zone as of the provided {@link java.time.Instant}.
 *
 * @param self    a ZoneId
 * @param instant an Instant
 * @return a ZoneOffset
 * @since 2.5.0
 */
public static ZoneOffset getOffset(final ZoneId self, Instant instant) {
  return self.getRules().getOffset(instant);
}

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

ZonedDateTime now = ZonedDateTime.now( ZoneId.of( "America/Montreal" ) );
…
ZoneId z = now.getZone();
ZoneRules zoneRules = z.getRules();
Boolean isDst = zoneRules.isDaylightSavings( now.toInstant() );

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

/**
   * Converts this TimeZone to a corresponding {@link java.time.ZoneOffset}. The offset is determined
   * using the date/time of specified Instant.
   *
   * @param self a TimeZone
   * @return a ZoneOffset
   * @since 2.5.0
   */
  public static ZoneOffset toZoneOffset(final TimeZone self, Instant instant) {
    return self.toZoneId().getRules().getOffset(instant);
  }
}

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