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

SimpleDateFormat throws parse exception when format mask has no year

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 6u29
    • core-libs
    • x86
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.6.0_24"
      Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
      Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows Ver 6.1

      A DESCRIPTION OF THE PROBLEM :
      If you create a SimpleDateFormat with a mask of MM/dd/YYYY it will parse a date of 02/29/2012.
      If you create a SimpleDateFormat with a mask of MM/dd it will throw a parse exception on 02/29.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See attached source

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I would expect the format to parse the date properly, assuming the current year.
      ACTUAL -
      An exception was thrown:

      java.text.ParseException: Unparseable date: "02/29 08:15"

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.text.ParseException: Unparseable date: "02/29 08:15"

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package com;

      /**
       * Created by IntelliJ IDEA.
       * User: drew.mcpheeters
       * Date: 2/29/12
       * Time: 2:05 PM
       * To change this template use File | Settings | File Templates.
       */
      import java.lang.*;
      import java.util.*;
      import java.text.*;

      public class SimpleDateFormatTester {

          public SimpleDateFormatTester()
          {
          }

          public static void main(String[] arg)
          {
              SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
              format.setLenient(false);
              SimpleDateFormat format2 = new SimpleDateFormat("MM/dd HH:mm");
              format2.setLenient(false);

              try
              {
                  String test1 = "02/29/2000 08:15:00";
                  String test2 = "02/29 08:15";
                  System.out.println("Test1:");
                  Date date1 = format.parse(test1);
                  System.out.println(date1);

                  System.out.println("Test2:");
                  Date date2 = format.parse(test2);
                  System.out.println(date2);

              }
              catch(ParseException pe)
              {
                  System.out.println(pe);
              }
              catch(Exception e)
              {
                  System.out.println(e);
              }
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      There are two work arounds.

      The first is to turn lenient off, but this may not always be desirable

      The second is to always include the year in the date mask, again this may not be desirable

      SUPPORT :
      YES

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

              Created:
              Updated:
              Resolved:
              Imported: