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

SimpleDateFormat yyyy-ww changes year of Date parsed, formatted, reparsed

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.2
    • core-libs
    • x86
    • windows_2000



      Name: rmT116609 Date: 12/10/2003


      FULL PRODUCT VERSION :
      java version "1.4.1_04"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_04-b01)
      Java HotSpot(TM) Client VM (build 1.4.1_04-b01, mixed mode)

      java version "1.4.2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
      Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)


      FULL OS VERSION :
      Microsoft Windows 2000 [Version 5.00.2195]

      A DESCRIPTION OF THE PROBLEM :
      SimpleDateFormatter("yyyy-ww", Locale.US) incorrectly parses the string "2003-01". Reformatting the result shows "2002-01", and the date constructed from that is different from the input date. I would expect that the parsed Date is invariant when sent in and out of the same date formatter.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the attached program.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I was expecting '2003-01' when parsed and formatted again to show '2003-01', and the Date object produced to show the same absolute time every time the parsed output is formatted and reparsed.


      ACTUAL -
      '2003-01' shows '2002-01'; parsing the latter doesn't produce the original time back. Works fine for '2003-02'

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

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

      public class SDFBug
      {
          public static void main( String[] args )
              throws java.text.ParseException
          {
              // run these in US locale
              testOne("2003-02", "yyyy-ww"); // ok
              testOne("2003-01", "yyyy-ww"); // fails
          }

          private static void testOne(String input, String format)
              throws java.text.ParseException
          {
              String header = "testOne [" + input + ", " + format +"]: ";
              java.text.DateFormat df = new java.text.SimpleDateFormat(format, java.util.Locale.US);
              java.util.Date date = df.parse(input);
              String output = df.format(date);
              System.out.println(header + "Input = " + input + ", output = " + output + "; date = " + date);
              java.util.Date date2 = df.parse(output);
              String output2 = df.format(date2);
              System.out.println(header + "Reparsed/formatted output = " + output2 + "; date2 = " + date);
              if (date.getTime() != date2.getTime())
                  System.out.println(header + "FAILED: date != date2");
              else
                  System.out.println(header + "OK");
          }
      }

      ---------- END SOURCE ----------
      (Incident Review ID: 217570)
      ======================================================================

            okutsu Masayoshi Okutsu
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: