为什么 inDaylightTime() 方法在时区返回错误?

Why inDaylightTime() method returning wrong in timezone?

我的第一个打印件显示夏令时从 3 月 8 日开始,而要检查我是否已将我的自定义日期(3 月 9 日)传递给 timeZone.inDaylightTime(calendar.getTime()),而它是返回 false.Where 我走错了?

    TimeZone timeZone = TimeZone.getTimeZone("US/Eastern");
    System.out.println("TimeZone ::: "+timeZone);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.AM_PM, Calendar.AM);
    calendar.set(Calendar.MONTH, Calendar.MARCH);
    calendar.set(Calendar.DAY_OF_MONTH, 9);
    calendar.set(Calendar.YEAR,2019);

    boolean b = timeZone.inDaylightTime(calendar.getTime());
    System.out.println("Is the day in daylight saving "+b);

DST 在 U.S 中未生效。东区2019年3月9日,3月10日2a.m生效。 See timeanddate.com.

My first print shows that daylight saving starts from 8th March...

我猜你是基于这个:

startMode=3, startMonth=2, startDay=8, startDayOfWeek=1, startTime=7200000

请记住,DST 更改(至少在 U.S.)总是发生在周日早上。该规则说的是,3 月 8 日或之后的第一个星期日是更改应该发生的时间,而不是它应该在 3 月 8 日 (这是一个星期五)发生。法律规定它从三月的第二个星期日开始(目前,它会定期变化),因此通过说第一个星期日在 3 月 8 日或之后,该代码符合“三月的第二个星期日”的定义。

你可以在SimpleTimeZone的描述中看到这个概念:

To construct a SimpleTimeZone with a daylight saving time schedule, the schedule can be described with a set of rules, start-rule and end-rule. A day when daylight saving time starts or ends is specified by a combination of month, day-of-month, and day-of-week values. The month value is represented by a Calendar MONTH field value, such as Calendar.MARCH. The day-of-week value is represented by a Calendar DAY_OF_WEEK value, such as SUNDAY. The meanings of value combinations are as follows.

  • Exact day of month To specify an exact day of month, set the month and day-of-month to an exact value, and day-of-week to zero. For example, to specify March 1, set the month to MARCH, day-of-month to 1, and day-of-week to 0.
  • Day of week on or after day of month To specify a day of week on or after an exact day of month, set the month to an exact month value, day-of-month to the day on or after which the rule is applied, and day-of-week to a negative DAY_OF_WEEK field value. For example, to specify the second Sunday of April, set month to APRIL, day-of-month to 8, and day-of-week to -SUNDAY.
  • Day of week on or before day of month To specify a day of the week on or before an exact day of the month, set day-of-month and day-of-week to a negative value. For example, to specify the last Wednesday on or before the 21st of March, set month to MARCH, day-of-month is -21 and day-of-week is -WEDNESDAY.
  • Last day-of-week of month To specify, the last day-of-week of the month, set day-of-week to a DAY_OF_WEEK value and day-of-month to -1. For example, to specify the last Sunday of October, set month to OCTOBER, day-of-week to SUNDAY and day-of-month to -1.

could you please post a small example by using those ZoneId and ZonedDateTime to get dayLightSaving date exists

我很乐意。

    ZoneId zone = ZoneId.of("America/New_York");
    ZonedDateTime testTime = ZonedDateTime.of(2019, 3, 9, 0, 0, 0, 0, zone);

    boolean dst = zone.getRules().isDaylightSavings(testTime.toInstant());
    System.out.println("Is the day in daylight saving " + dst);

Is the day in daylight saving false

正如另一个答案中所述,夏令时 (DST) 从今年 3 月 10 日的上周日在北美开始。因此,让我们在那天早上 5 点尝试 — 然后我们应该得到 true.

    ZonedDateTime testTime = ZonedDateTime.of(2019, 3, 10, 5, 0, 0, 0, zone);

Is the day in daylight saving true

zone.getRules() returns 一个 ZoneRules 对象。这样的对象知道有关时区的 UTC 偏移量的所有信息,也了解 DST。所以我们可以用 Instant 查询它。我们从 testTime.toInstant().

得到一个 Instant

Link: Oracle 教程中的 Time Zone and Offset Classes 部分(包括一个使用 ZoneRules 的示例,与我的有点相似)。