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

SimpleDateFormat year patterns don't behave as in documentation

XMLWordPrintable

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

      FULL PRODUCT VERSION :
      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 :
      this is an API-problem, not OS-specific.

      A DESCRIPTION OF THE PROBLEM :
      The Documentation of SimpleDateFormat
      http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html#year

      says:

      | Year: For formatting, if the number of pattern letters is 2,
      | the year is truncated to 2 digits; otherwise it is interpreted
      | as a number.

      So when formatting a date in year 2003 whith a pattern consisting
      of exactly one letter (or exactly 3 letters), the year should be
      treated as a number (and thus printed truncated).

      The current implementation in both cases truncates the year
      to two digits (03).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the class given below.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Output should be:

      2003
      2003
      ACTUAL -
      Output is

      03
      03

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.Date;
      import java.text.SimpleDateFormat;

      class Beispiel
      {
      public static void main(String[] params)
      {
      Date d = new Date(1065306927040L);
      System.out.println(new SimpleDateFormat("y").format(d));
      System.out.println(new SimpleDateFormat("yyy").format(d));
      }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      One could use the pattern "yyyy" for 4-digit-years,
      but this would format with leading zeros when given
      a year with 3 or fewer digits. Additionally, it has
      other impacts on parsing.

            peytoia Yuka Kamiya (Inactive)
            okutsu Masayoshi Okutsu
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: