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

Inconsistent two-digit date parsing in SimpleDateFormat

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P1 P1
    • None
    • 1.2.0
    • core-libs
    • x86
    • generic

      SimpleDateFormat's parse method returns a different date from the date that is stored in the object afterwards. This is demonstrated in the following code:

          DateFormat sdf = new SimpleDateFormat("M/d/yy");
          DateFormat output = new SimpleDateFormat("M/d/yyyy");
          {
              String s = "1/1/18";
              Date d = sdf.parse(s);
              Calendar c = sdf.getCalendar();
              System.out.println("Parsing "+s+" to get Date and Calendar objects");
              System.out.println("date = "+output.format(d));
              System.out.println("calendar = "+output.format(c.getTime()));
          }
          System.out.println("");
          {
              String s = "12/31/18";
              System.out.println("Parsing "+s+" to get Date and Calendar objects");
              Date d = sdf.parse(s);
              Calendar c = sdf.getCalendar();
              System.out.println("date = "+output.format(d));
              System.out.println("calendar = "+output.format(c.getTime()));
          }

      The Calendar value retrieved from SimpleDateFormat.getCalendar() is based on the year only, whereas the Date value returned from SimpleDateFormat.parse() is based on the instant when the object was created.

      The bug exists in all investigated versions of JDK from 1.1.5 to 1.2.

      See http://javaweb.israel/javapc_qa/Y2K/ComplianceNotes.html for more details.

            aliusunw Alan Liu (Inactive)
            vrosenmasunw Victor Rosenman (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: