SimpleDateFormat y2k behavior odd

XMLWordPrintable

    • Type: Bug
    • Resolution: Won't Fix
    • Priority: P2
    • None
    • Affects Version/s: 1.2.0
    • Component/s: core-libs
    • generic
    • generic



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

            Assignee:
            Alan Liu (Inactive)
            Reporter:
            Brian Beck (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: