Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4208960

TimeZone.getOffset doesn't work with february 29th

    XMLWordPrintable

Details

    • b03
    • generic, x86
    • generic, windows_nt

    Backports

      Description



        Name: gsC80088 Date: 02/04/99


        With Java 2, NT4.0 and the system time zone set to Eastern time Us&Canada, and daylight saving on,
        the getOffset function returns an IllegalArgumentException for no apparent reasons.
        The same code works with the time zone set to GMT UK time.

        import java.util.*;

        public class TestTimeZone {

          public static void main(String[] args) {

            SimpleTimeZone tz = (SimpleTimeZone)Calendar.getInstance().getTimeZone();
            System.out.println("Offset for february 28, 1996: " + tz.getOffset(GregorianCalendar.AD, 1996, 1, 28, Calendar.WEDNESDAY, 0));
            System.out.println("Offset for february 29, 1996: " + tz.getOffset(GregorianCalendar.AD, 1996, 1, 29, Calendar.THURSDAY, 0));
          }

        }
        (Review ID: 49133)
        ======================================================================

        Name: vi73552 Date: 05/20/99


        The getOffset(...) method in SimpleTimeZone throws an InvalidArgumentException when it is passed a date of Feb. 29th during a leap year (tested both Feb 29th 1996 and Feb 29th 2000). The reason for this appears to be that the SimpleTimeZone class uses a simple validation routine that doesn't handle leap years.


        Code to reproduce the problem:


        import java.util.*;
        import java.text.*;

        public class DateTest
        {
          public static void main(String[] args)
            {
            try
              {
              SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
              Date d = dateFormatter.parse("02/29/1996");
              TimeZone tz = TimeZone.getTimeZone("PST");
              Calendar c = Calendar.getInstance();
              c.setTime(d);
              int timeOfDay = c.get(Calendar.HOUR_OF_DAY) * 60 * 60 *1000;
              timeOfDay += c.get(Calendar.MINUTE) * 60 * 1000;
              timeOfDay += c.get(Calendar.SECOND) * 1000;

              int offset = tz.getOffset(c.get(Calendar.ERA), c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.DAY_OF_WEEK), timeOfDay);

              System.out.println("Got an offset of: " + offset);
              }
            catch (Exception e)
              {
              e.printStackTrace(System.out);
              }
            }
        }


        output of the program is:

        java.lang.IllegalArgumentException
        at java.util.SimpleTimeZone.getOffset(Compiled Code)
        at java.util.SimpleTimeZone.getOffset(SimpleTimeZone.java:356)
        at DateTest.main(DateTest.java:19)


        This is with java -version:
        java version "1.2"
        Classic VM (build JDK-1.2-V, native threads)

        And with java -fullversion:
        java full version "JDK-1.2-V"


        Upon examination of the code in SimpleTimeZone.java, we discovered that it uses the following array to determine the lengths of the various months. If it finds a day_of_month outside this range, it throws an IllegalArgumentException.


        ======================================================================

        Name: krT82822 Date: 09/19/99


        SUB: Y2K Leap Year related error in MailApi class MimeMessage

        JavaMailApiVersion: 1.1.x
        OS: Windows NT 4.0 Service Pack 3 or greater
        Platform: Intel Based

        MimeMessage class setSentDate(new Date()) method causes the
        following Error if you set the system date to Year 2000, Feb 29.

        // Code
           MimeMessage msg = new MimeMessage(outSession);
           // Set the Date: header causes the Error
           msg.setSentDate(new Date());

        ERROR:
        =====

        java.lang.IllegalArgumentException
                at java.util.SimpleTimeZone.getOffset(SimpleTimeZone.java:395)
                at java.util.SimpleTimeZone.getOffset(SimpleTimeZone.java:356)
                at com.sun.mail.util.MailDateFormat.format(MailDateFormat.java:143)
                at java.text.DateFormat.format(DateFormat.java:312)
                at javax.mail.internet.MimeMessage.setSentDate(MimeMessage.java:587)
                at com.hp.ts.server.MailProcessor.OutMailMessage.deliverMessage(OutMailM
        essage.java:440)
                at com.hp.ts.server.MailProcessor.OutMailMessage.sendMessage(OutMailMess
        age.java:249)
                at com.hp.ts.server.MailProcessor.OutMailMessage.sendMessage(OutMailMess
        age.java:213)
        (Review ID: 94259)
        ======================================================================

        Attachments

          Issue Links

            Activity

              People

                ktakahassunw Koushi Takahashi (Inactive)
                gstone Greg Stone
                Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: