gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:11:02 26 4
gpt4 key购买 nike

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

    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 于 2019 年 3 月 9 日在美国东部地区未生效。它于 3 月 10 日凌晨 2 点生效。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 更改(至少在美国是这样)总是在星期日清晨发生。该规则的意思是,更改应该在 3 月 8 日或之后的第一个星期日发生,而不是应该在 3 月 8 日(星期五)发生。法律规定它从 3 月的第二个星期日开始(目前,它会定期变化),因此通过说第一个星期日在 3 月 8 日或之后,该代码符合“3 月的第二个星期日”的定义。

你可以在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 monthTo 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 monthTo 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 monthTo 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 monthTo 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.

关于java - 为什么 inDaylightTime() 方法在时区返回错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55139831/

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