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

java.text.SimpleDateFormat.parse throws unexpected exception

XMLWordPrintable

    • sparc
    • solaris_2.5



      Name: dfC67450 Date: 01/16/98



      The java.text.SimlpeDateFormat.parse(String, ParsePosition) throws unexpected
      StringIndexOutOfBoundsException when String parameter contains incorrectly
      formatted date.
      Javadoc says about java.text.DateFormat.parse(String text, ParsePosition pos) method:
           *
           * @return A Date, or null if the input could not be parsed
           *

      Here is the test demonstrating the bug:

      -----------------Test.java------------------------
      import java.text.*;
      import java.util.*;
      public class Test {
          public static void main (String args[]){
              SimpleDateFormat sdf = new SimpleDateFormat();
              String pattern = "'time' hh:mm";
              sdf.applyPattern(pattern);
              System.out.println("pattern: \"" + pattern + "\"");
              System.out.println();

            // works correctly
              ParsePosition pp = new ParsePosition(0);
              String text = "time ";
              System.out.println(" text: \"" + text + "\"");
              Date date = sdf.parse(text, pp);
              System.out.println(" date: " + date);
              System.out.println();

            // works wrong
              pp = new ParsePosition(0);
              text = "time";
              System.out.println(" text: \"" + text + "\"");
              date = sdf.parse(text, pp);
              System.out.println(" date: " + date);

         }

      }

      ---------Output from the test---------------------
      pattern: "'time' hh:mm"
       
        text: "time "
        date: null
       
        text: "time"
      java.lang.StringIndexOutOfBoundsException: String index out of range: 4
              at java.lang.String.charAt(String.java)
              at java.text.SimpleDateFormat.parse(SimpleDateFormat.java)
              at Test.main(Test.java:23)
      ------------------------------------------------

      ======================================================================

            aliusunw Alan Liu (Inactive)
            dfazunensunw Dmitri Fazunenko (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: