JDK-4167513 : SimpleDateFormat y2k behavior odd
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 1.2.0
  • Priority: P2
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic
  • CPU: generic
  • Submitted: 1998-08-19
  • Updated: 1998-09-16
  • Resolved: 1998-09-16
Description

Name: bb33257			Date: 08/19/98


SimpleDateFormat handles 2-digit years by forcing them into a
window.  The window spans d-80years to d+20years, where d is the
time at which the object is created.

This leads to odd behavior.

Two SimpleDateFormat objects, created one after the other, using
identical parameters, do not compare as equal -- since their
windows are slightly different.  Furthermore, the objects don't
behave the same, for the same reason.  Here is a test program:

import java.text.*;
import java.util.*;
public class dfbug {
    public static void main(String[] args) throws Exception {
        long gap = 2000; // ms between DateFormat creations

        DateFormat df1 = DateFormat.getDateTimeInstance(
            DateFormat.SHORT, DateFormat.LONG, Locale.US);
        Thread.sleep(gap);
        DateFormat df2 = DateFormat.getDateTimeInstance(
            DateFormat.SHORT, DateFormat.LONG, Locale.US);
        
        Date crux = new Date(
            ((SimpleDateFormat)df1).get2DigitYearStart().getTime() + gap/2);
        
        String s = df1.format(crux);

        Date d1 = df1.parse(s);
        Date d2 = df2.parse(s);
        System.out.println(d1);
        System.out.println(d2);
        System.out.println(d1.equals(d2) ? "ok" : "FAIL");

        System.out.println("Formats equal: " +
                           df1.equals(df2));
    }
}

Here is the output.  Note that the two objects parse the same
string differently:

Mon Aug 19 10:37:48 PDT 1918
Sun Aug 19 10:37:48 PDT 2018
FAIL
Formats equal: false

I propose that SimpleDateFormat be modified in a simple way --
the 100-year window boundary should be rounded down to midnight,
local time.  That way, most DateFormat objects created in
succession will (1) be equal, and (2) behave identically.  Only
objects created on different days will behave differently.

This change will stay within the spec (the javadoc for SDF).
Currently, the spec states "It does this by adjusting dates to be
within 80 years before and 20 years after the time the
SimpleDateFormat instance is created."  We may wish to change the
word "time" to "date."

This will also fix the somewhat bizarre behavior that a two digit
year may be parsed differently depending on the TIME OF DAY.

The change itself should only involve a few lines of code.
======================================================================

Comments
EVALUATION JavaSoft (bcbeck@eng, jbloch@eng) decided not to change the current behavior. norbert.lindenberg@Eng 1998-09-16
11-06-2004

WORK AROUND Name: bb33257 Date: 08/19/98 Use SimpleDateFormat.set2DigitYearStart() to set the date to a known boundary after creating the object -- but clients should not have to do this! ======================================================================
11-06-2004