My first print shows that daylight saving starts from 8th March, whereas to check that I have passed my custom date(9th March) to timeZone.inDaylightTime(calendar.getTime()), whereas it is returning false.Where I am going wrong ?
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 wasn't in effect in the U.S. Eastern zone on March 9th, 2019. It went into effect March 10th at 2 a.m. See timeanddate.com.
My first print shows that daylight saving starts from 8th March...
I'm guessing you're basing that on this:
startMode=3, startMonth=2, startDay=8, startDayOfWeek=1, startTime=7200000
Remember that DST changes (in the U.S. at least) always happen early on a Sunday morning. What that rule says is that the first Sunday on or after March 8th is when the change should occur, not that it should occur on March 8th (which was a Friday). The law says it starts the second Sunday in March (at the moment, it's varied periodically), so by saying the first Sunday on or after March 8th, the code fits the definition "second Sunday of March."
You can see this concept in the description of 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.