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

SimpleDateFormat: doc/implementation inconsistent for number of digits for year

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.3.0
    • core-libs
    • None
    • beta
    • generic
    • generic

      Run the test case below. According to the javadoc, years should be formatted according to the rule for "Number" (that is, the number of format characters specifies the minimum number of digits, with zero-padding), except for 'yy', where the year is truncated to two digits. The implementation produces different results. Below are expected and actual results for the year 2000:

      Format string Expected result Actual result
      ------------- --------------- -------------
      y 2000 00
      yy 00 00
      yyy 2000 00
      yyyy 2000 2000
      yyyyy 02000 2000



      import java.text.SimpleDateFormat;
      import java.util.Date;


      public class YearFormatTest {
          public static void main(String args[]) {
            SimpleDateFormat sdf = new SimpleDateFormat();
            Date today = new Date();

            sdf.applyPattern("MM d y");
            System.out.println(sdf.format(today));

            sdf.applyPattern("MM d yy");
            System.out.println(sdf.format(today));

            sdf.applyPattern("MM d yyy");
            System.out.println(sdf.format(today));

            sdf.applyPattern("MM d yyyy");
            System.out.println(sdf.format(today));

            sdf.applyPattern("MM d yyyyy");
            System.out.println(sdf.format(today));
          }
      }

            peytoia Yuka Kamiya (Inactive)
            nlindenbsunw Norbert Lindenberg (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: