JDK-4966499 : SimpleDateFormat yyyy-ww changes year of Date parsed, formatted, reparsed
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2003-12-10
  • Updated: 2004-01-13
  • Resolved: 2004-01-13
Related Reports
Duplicate :  
Relates :  
Description

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) 
======================================================================

Comments
EVALUATION I've modified the test case to show each parse/format step. The result is: testOne [2003-02, yyyy-ww]: parsing '2003-02' produced Sun Jan 05 00:00:00 PST 2003 formatting 'Sun Jan 05 00:00:00 PST 2003' produced 2003-02 parsing '2003-02' produced Sun Jan 05 00:00:00 PST 2003 formatting 'Sun Jan 05 00:00:00 PST 2003' produced 2003-02 testOne [2003-02, yyyy-ww]: OK testOne [2003-01, yyyy-ww]: parsing '2003-01' produced Sun Dec 29 00:00:00 PST 2002 formatting 'Sun Dec 29 00:00:00 PST 2002' produced 2002-01 parsing '2002-01' produced Sun Dec 30 00:00:00 PST 2001 formatting 'Sun Dec 30 00:00:00 PST 2001' produced 2001-01 testOne [2003-01, yyyy-ww]: FAILED: date != date2 Parsing "Sun Dec 29 00:00:00 PST 2002" produces "2002-1" because the year value is 2002 and the WEEK_OF_YEAR value is 1 (in 2003). This can't be fixed without the week-based year support. Closing this bug as a duplicate of 4267450. ###@###.### 2004-01-13
13-01-2004