Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8781503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:19:13+00:00 2026-06-13T20:19:13+00:00

I Have an issue with the use of SimpleTimeZone class in Java. First, the

  • 0

I Have an issue with the use of SimpleTimeZone class in Java. First, the JavaDoc is nice but not quite easy to understand in regards of the start and end Rules. But with the help of some example found on the web, i managed to get it right (i still don’t understand why 8 represents the second week of a month in day_of_month!!! but whatever)

Now i have written a simple Junit test to validate what i understand:

package test;

import static org.junit.Assert.assertEquals;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.SimpleTimeZone;

import org.apache.log4j.Logger;
import org.junit.Test;



public class SimpleTimeZoneTest {

Logger log = Logger.getLogger(SimpleTimeZoneTest.class);

@Test   
public void testTimeZoneWithDST() throws Exception {


    Calendar testDateEndOut = new GregorianCalendar(2012, Calendar.NOVEMBER, 4, 01, 59, 59);
    Calendar testDateEndIn = new GregorianCalendar(2012, Calendar.NOVEMBER, 4, 02, 00, 00);
    Calendar testDateStartOut = new GregorianCalendar(2012, Calendar.MARCH, 11, 01, 59, 59);
    Calendar testDateStartIn = new GregorianCalendar(2012, Calendar.MARCH, 11, 02, 00, 00);

    SimpleTimeZone est = new SimpleTimeZone(-5 * 60 * 60 * 1000, "EST");
    est.setStartRule(Calendar.MARCH, 8, -Calendar.SUNDAY, 2 * 60 * 60 * 1000);
    est.setEndRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);

    Calendar theCal = new GregorianCalendar(est);

    theCal.setTimeInMillis(testDateEndOut.getTimeInMillis());

    log.info(" Cal date = " + new Timestamp(theCal.getTimeInMillis()) + " : " + theCal.getTimeZone().getDisplayName());
    log.info(" Cal use DST = " + theCal.getTimeZone().useDaylightTime());
    log.info(" Cal In DST = " + theCal.getTimeZone().inDaylightTime(theCal.getTime()));
    log.info("offset = " + theCal.getTimeZone().getOffset(theCal.getTimeInMillis()));
    log.info("DTS offset= " + theCal.getTimeZone().getDSTSavings());
    assertEquals("End date Should be In DST", true, theCal.getTimeZone().inDaylightTime(theCal.getTime()));

    theCal.setTimeInMillis(testDateEndIn.getTimeInMillis());

    log.info(" Cal date = " + new Timestamp(theCal.getTimeInMillis()) + " : " + theCal.getTimeZone().getDisplayName());
    log.info(" Cal use DST = " + theCal.getTimeZone().useDaylightTime());
    log.info(" Cal In DST = " + theCal.getTimeZone().inDaylightTime(theCal.getTime()));
    log.info("offset = " + theCal.getTimeZone().getOffset(theCal.getTimeInMillis()));
    log.info("DTS offset= " + theCal.getTimeZone().getDSTSavings());
    assertEquals("End date Should be Out DST", false, theCal.getTimeZone().inDaylightTime(theCal.getTime()));

    theCal.setTimeInMillis(testDateStartIn.getTimeInMillis());

    log.info(" Cal date = " + new Timestamp(theCal.getTimeInMillis()) + " : " + theCal.getTimeZone().getDisplayName());
    log.info(" Cal use DST = " + theCal.getTimeZone().useDaylightTime());
    log.info(" Cal In DST = " + theCal.getTimeZone().inDaylightTime(theCal.getTime()));
    log.info("offset = " + theCal.getTimeZone().getOffset(theCal.getTimeInMillis()));
    log.info("DTS offset= " + theCal.getTimeZone().getDSTSavings());
    assertEquals("Start date Should be in DST", true, theCal.getTimeZone().inDaylightTime(theCal.getTime()));

    theCal.setTimeInMillis(testDateStartOut.getTimeInMillis());

    log.info(" Cal date = " + new Timestamp(theCal.getTimeInMillis()) + " : " + theCal.getTimeZone().getDisplayName());
    log.info(" Cal use DST = " + theCal.getTimeZone().useDaylightTime());
    log.info(" Cal In DST = " + theCal.getTimeZone().inDaylightTime(theCal.getTime()));
    log.info("offset = " + theCal.getTimeZone().getOffset(theCal.getTimeInMillis()));
    log.info("DTS offset= " + theCal.getTimeZone().getDSTSavings());
    assertEquals("Start date Should be Out DST", false, theCal.getTimeZone().inDaylightTime(theCal.getTime()));





}

}

Ok, i want to test the date limits to see if the inDaylightTime return the right thing!

So, my rules are :

DST start the second sunday of March at 2am
DST end the first sunday of november at 2am

In 2012 (now) this give us the march 11 at 2am and November 4 at 2am

You can see my test dates are set properly!!!

Well here is the output of my test run:

2012-11-01 18:22:44,344 INFO [test.SimpleTimeZoneTest] - < Cal date = 2012-11-04 01:59:59.0 : Eastern Standard Time>
2012-11-01 18:22:44,345 INFO [test.SimpleTimeZoneTest] - < Cal use DST = true>
2012-11-01 18:22:44,345 INFO [test.SimpleTimeZoneTest] - < Cal In DST = false>
2012-11-01 18:22:44,345 INFO [test.SimpleTimeZoneTest] - <offset = -18000000>
2012-11-01 18:22:44,345 INFO [test.SimpleTimeZoneTest] - <DTS offset= 3600000>

My first assert just fails and tell me that 2012-11-04 01:59:59 is not inDST… !!!!???

If i put 2012-11-04 00:59:59, the test pass!

This 1 hour gap just puzzle me… can anyone explain this behavior?

Oh, btw, if anyone could elaborate on the :

est.setStartRule(Calendar.MARCH, 8, -Calendar.SUNDAY, 2 * 60 * 60 * 1000);

Why 8 means second week of march… and the -SUNDAY. I can’t figure out this thing on a real calendar example!!!

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:19:14+00:00Added an answer on June 13, 2026 at 8:19 pm

    My first assert just fails and tell me that 2012-11-04 01:59:59 is not inDST… !!!!???

    On November 4th at what-would-be 2:00 AM, we "fall back" to 1:00 AM, so the time 1:59:59 happens twice, once within DST and once after DST; so, this statement:

    Calendar testDateEndOut =
        new GregorianCalendar(2012, Calendar.NOVEMBER, 4, 01, 59, 59);
    

    is genuinely ambiguous. As it happens, the JDK decides that it refers to the second occurrence of 1:59:59, the one that’s outside DST, which is why you’re seeing that result.

    Edited to add: You could remove this ambiguity, and make certain you get the first occurrence, by initially setting testDateEndOut to 00:00:00 and then writing testDateEndOut.setTimeInMillis (testDateEndOut.getTimeInMillis() + 7199000L) to move it forward by the desired amount.

    Oh, btw, if anyone could elaborate on the :

    est.setStartRule(Calendar.MARCH, 8, -Calendar.SUNDAY, 2 * 60 * 60 * 1000);
    

    Why 8 means second week of march… and the -SUNDAY.

    java.util.SimpleTimeZone supports a lot of different cases, so it uses some magic. You’re using the four-argument version of setStartRule, so your arguments are as follows:

    • startMonth – you already understand this. MARCH means March.
    • startDay – note: day, not week. 8 means the eighth day of the Month.
    • startDayOfWeek – you use a negative value to indicate that you want a day on or after the above-specified date. -SUNDAY means a Sunday on or after.
    • startTime – you understand this.

    Altogether, (Calendar.MARCH, 8, -Calendar.SUNDAY, 2 * 60 * 60 * 1000) means 2:00 AM on the first Sunday on or after March 8th — which is the Sunday in the range [March 8th, March 14th] — which is the second Sunday in March.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue regarding Sendkeys Class, as i want to use this class
I have one issue when I am trying to use setValidator() (not setValidators )
I have issue about convert XML to Java Object , in here i use
i have an issue i could use some help with, i have python list
I have an issue with an object I created and then use in a
I am completely stumped with how to use .htaccess and I have an issue
I rarely use SQL and have a unique issue I need to solve. I
I have a weird issue with printing data out. I use printf to print
hello i have a silly issue, my link isn't shown. i use the same
I have an issue with imagecopyresampled for buffers that use alpha values. apparently the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.