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

[Fmt-Da] [Doc] Inconsistent results when parsing Strings with SimpleDateFormat

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 1.1.7, 1.2.2
    • core-libs
    • Cause Known
    • generic
    • generic



      Name: dbT83986 Date: 04/06/99


      My JDK info:
      java version "1.1.7B"
      java full version "JDK1.1.7U"

      My issue is that SimpleDateFormat parse() does not throw a ParseException in those cases where it should.

      There are two situations: parsing the String "4F" with the parse pattern "HH" and parsing the String "4:2" with the parse pattern "hh:mm". (BTW, I know that this appears to be two issues, but they are closely related. Spank me if I should have submitted these separately.)

      The problem in both cases is that the developer has to do a certain amount of prep work on the String before passing it to SDF.parse(). In the first case, I need to check for alpha characters mixed in with numeric characters. Note that checking for alpha chars is not something I need to do when using other patterns (e.g. hh:mm a). And in the second case, I need to check that the value for minutes is entered as two digits. Shouldn't 4:2 always be treated as invalid since it is ambiguously 4:20 or 4:02? (At the very least, it should be parsed as 4:20).

      Here is sample code for both problems:

      // parsing alpha chars with HH pattern:

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

      public class SDFIssueOne
      {
      public static void main( String[] args )
      {
      String parsePattern = "HH";
      SimpleDateFormat sdf = new SimpleDateFormat(parsePattern);

      String[] inputs = { "1", // works correctly
      "15", // works correctly
      "1A", // should fail but doesn't
      "4F", // should fail but doesn't
      "A3", // fails correctly
      "junk" }; // fails correctly
      Date retDate = null;

      for( int i = 0; i < inputs.length; i++ ) {
      try
      {
      sdf.setLenient(false);
      retDate = sdf.parse(inputs[i]);
      System.out.println( "Parsed date " + inputs[i] + " is " + retDate );
      }
      catch (ParseException e)
      { System.out.println( "Parse exception for " + inputs[i]);
      System.out.println( e.getMessage() );
      }
      }
      }
      }

      // parsing ambiguous minutes with "hh:mm":

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

      public class SDFIssueTwo
      {

      public static void main( String[] args )
      {
      String parsePattern = "hh:mm";
      SimpleDateFormat sdf = new SimpleDateFormat(parsePattern);

      // should fail - only last fail:
      String[] inputs = { "3:45", // works as expected
      "4:2", // DOESN'T throw exception (it should) - produces 4:02.
      "5:234", // throws exception (correctly)
      "11:" }; // throws exception (correctly)
      Date retDate = null;

      for( int i = 0; i < inputs.length; i++ ) {
      try
      {
      sdf.setLenient(false);
      retDate = sdf.parse(inputs[i]);
      System.out.println( "Parsed date " + inputs[i] + " is " + retDate );
      }
      catch (ParseException e)
      { System.out.println( "Parse exception for " + inputs[i]);
      System.out.println( e.getMessage() );
      }
      }
      }
      }
      (Review ID: 56588)
      ======================================================================

            naoto Naoto Sato
            dblairsunw Dave Blair (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: