Java 中的自定义 ZoneIds/时区

geo*_*tle 2 java time icalendar timezone

我正在尝试VTIMEZONE使用 JavaZoneIdZoneOffsetTransitionRule.

我的VTIMEZONE对象看起来像

BEGIN:VTIMEZONE
TZID:Central European Standard Time
BEGIN:STANDARD
DTSTART:16010101T030000
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=10
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:16010101T020000
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
RRULE:FREQ=YEARLY;INTERVAL=1;BYMONTH=1;BYDAY=MO
END:DAYLIGHT
END:VTIMEZONE
Run Code Online (Sandbox Code Playgroud)

我需要创建自己ZoneId的模型来对此进行建模,因为据我所知,ZoneId这些偏移量不可用,并且 DST 开始于 1 月的第一个星期一(而不是 3 月的某个星期日)。

我有以下创建一个 ZoneOffsetTransitionRule

ZoneOffsetTransitionRule of =
  ZoneOffsetTransitionRule.of(Month.JANUARY, 1, DayOfWeek.MONDAY, LocalTime.of(2, 0),
    false, ZoneOffsetTransitionRule.TimeDefinition.STANDARD, ZoneOffset.ofHours(1),
    ZoneOffset.ofHours(1), ZoneOffset.ofHours(2));
Run Code Online (Sandbox Code Playgroud)

但我不确定它是否正确或如何从中创建一个ZoneId

  • 该转换规则是否准确地模拟了DAYLIGHTmy的组件VTIMEZONE
  • 我怎样才能ZoneId从这个创建一个这样我最终可以创建一个ZonedDateTime

Ole*_*.V. 6

只有这样,才能得到一个ZoneId(至少如果我们不是非常哈克)是通过工厂方法ZoneId和它的子类ZoneOffset。乍一看,这似乎与内置ZoneIds 无关。但是,有一个后门用于指定可以产生的附加ZoneIds ZoneId.of。它被称为ZoneRulesProvider。我们需要指定一个新的唯一 ID,我们需要指定区域规则(因此名称为ZoneRulesProvider)。

所以有了你,ZoneOffsetTransitionRule你已经在路上了。但是,我们需要其中的两个,用于过渡到夏令时(通常发生在春季)和一个用于在秋季进行相反的操作。

当然,以下清单不是生产代码,只是为了证明开发和注册您自己的ZoneRulesProvider.

    final String customZoneId = "Custom-CEST-1";

    final ZoneOffset standardOffset = ZoneOffset.ofHours(1);
    final ZoneOffset summerTimeOffset = ZoneOffset.ofHours(2);
    // At least one transistion is required
    ZoneOffsetTransition initialTransition = ZoneOffsetTransition.of(
            LocalDateTime.of(1601, 1, 1, 3, 0), summerTimeOffset, standardOffset);
    List<ZoneOffsetTransition> transitionList = List.of(initialTransition);

    // Rules for going to and from summer time (DST)
    ZoneOffsetTransitionRule springRule =
            ZoneOffsetTransitionRule.of(Month.JANUARY, 1, DayOfWeek.MONDAY, LocalTime.of(2, 0),
                    false, ZoneOffsetTransitionRule.TimeDefinition.STANDARD, standardOffset,
                    standardOffset, summerTimeOffset);
    ZoneOffsetTransitionRule fallRule =
            ZoneOffsetTransitionRule.of(Month.OCTOBER, -1, DayOfWeek.SUNDAY, LocalTime.of(2, 0),
                    false, ZoneOffsetTransitionRule.TimeDefinition.STANDARD, standardOffset,
                    summerTimeOffset, standardOffset);
    ZoneRules rules = ZoneRules.of(standardOffset, standardOffset,
            transitionList, transitionList, List.of(springRule, fallRule));

    // The heart of the magic: the ZoneRulesProvider
    ZoneRulesProvider customProvider = new ZoneRulesProvider() {

        @Override
        protected Set<String> provideZoneIds() {
            return Set.of(customZoneId);
        }

        @Override
        protected NavigableMap<String, ZoneRules> provideVersions(String zoneId) {
            return new TreeMap<>(Map.of(customZoneId, rules));
        }

        @Override
        protected ZoneRules provideRules(String zoneId, boolean forCaching) {
            return rules;
        }
    };

    // Registering the ZoneRulesProvider is the key to ZoneId using it
    ZoneRulesProvider.registerProvider(customProvider);

    // Get an instance of our custom ZoneId
    ZoneId customZone = ZoneId.of(customZoneId);
    // Transition to standard time was Sunday, October 29, 2017,
    // so try the day before and the day after
    System.out.println(LocalDate.of(2017, Month.OCTOBER, 28).atStartOfDay(customZone));
    System.out.println(LocalDate.of(2017, Month.OCTOBER, 30).atStartOfDay(customZone));
    // The special thing about our custom ZoneID is that transition to DST
    // happened on Monday, January 1. Try the day before and the day after.
    System.out.println(LocalDate.of(2017, Month.DECEMBER, 31).atStartOfDay(customZone));
    System.out.println(LocalDate.of(2018, Month.JANUARY, 2).atStartOfDay(customZone));
Run Code Online (Sandbox Code Playgroud)

代码打印:

2017-10-28T00:00+02:00[Custom-CEST-1]
2017-10-30T00:00+01:00[Custom-CEST-1]
2017-12-31T00:00+01:00[Custom-CEST-1]
2018-01-02T00:00+02:00[Custom-CEST-1]
Run Code Online (Sandbox Code Playgroud)

我们看到,在转换到标准时间之前和转换到夏令时之后,我们得到了 +02:00 的预期 DST 偏移量。