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

java.util.Date.parse() does not follow the language spec for dates like 02/14/96

XMLWordPrintable

    • 1.1
    • generic, sparc
    • solaris_2.5
    • Not verified

      java.util.Date.parse() does not parse dates of the form "2/4/96" correctly.

      The Java Language Specification has a very detailed description of the Date
      class' parse method in chapter 20, section 20.3.31. In its discussion of
      how it treats numbers that it finds in its argument it says:

          If the number is followed by a slash, it is regarded as a month (it is
          decreased by 1 to produce a number in the range 0 to 11), unless a month
      has
          already been recognized, in which case it is regarded as a day of the month.

      The following code reproduces the problem.

         import java.util.Date;
          public class DateParse {
              public static void main(String argv[]) {
                  Date d = new Date("2/4/96");
                  System.out.println("getMonth() returns " + d.getMonth());
                  System.out.println(d);
              }
          }

      Output:

      java DateParse
      getMonth() returns 2
      Mon Mar 04 00:00:00 EST 1996


      According to the spec parse() should have decremented the month by one
      to provide 0 based enumeration. getMonth() should have returned 1 and the month Feb.
      Correct output should have been:

      java DateParse
      getMonth() returns 1
      Mon Feb 04 00:00:00 EST 1996



            duke J. Duke
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: