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

java.text.SimpleDateFormat.parse() incorrectly sets ParsePosition index

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      sparc
    • OS:
      solaris_2.5

      Description



      Name: dfC67450 Date: 01/15/98



      The java.text.SimlpeDateFormat.parse(String, ParsePosition) incorrectly sets
      ParsePosition index when String parameter contains incorrectly formatted date.
      Javadoc says about java.text.DateFormat.parse(String text, ParsePosition pos) method:

           * @param pos On input, the position at which to start parsing; on
           * output, the position at which parsing terminated, or the
           * start position if the parse failed.

      Here is the test demonstrating the bug:

      -----------------Test.java------------------------
      import java.text.*;
      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 + "\"");

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

            // works correctly
              pp = new ParsePosition(0);
              text = "time 10:x";
              sdf.parse(text, pp);
              System.out.println(" text: \"" + text + "\"");
              System.out.println(" index: " + pp.getIndex());

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

         }

      }

      ---------Output from the test---------------------
      pattern: "'time' hh:mm"
        text: "time 10:30"
        index: 10
        text: "time 10:x"
        index: 8
        text: "time 10x"
        index: 0
      ------------------------------------------------

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

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: