5 Replies Latest reply: Dec 24, 2012 7:26 AM by 980759 RSS

    Date fromat parsing in java

    mjmjava
      Hello,
      I have a following code

      String fromDate = "20/20/2009";
           DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
           java.util.Date dtt = df.parse(fromDate);
           java.sql.Date ds = new java.sql.Date(dtt.getTime());
           System.out.println(ds);

      i get output like " 2010-08-20" why i does not get parsing error , instead i get wrongly parsed date

      Is it bug with java platform ?
        • 1. Re: Date fromat parsing in java
          980759
          please go through the doc SimpleDateFormat and source code, you will come to know,
          parse() method throws exception when you did not pass anything to parse method.

          http://docs.oracle.com/javase/6/docs/api/index.html?java/text/SimpleDateFormat.html
          /**
               * Parses text from the beginning of the given string to produce a date.
               * The method may not use the entire text of the given string.
               * <p>
               * See the {@link #parse(String, ParsePosition)} method for more information
               * on date parsing.
               *
               * @param source A <code>String</code> whose beginning should be parsed.
               * @return A <code>Date</code> parsed from the string.
               * @exception ParseException if the beginning of the specified string
               *            cannot be parsed.
               */
              public Date parse(String source) throws ParseException
              {
                  ParsePosition pos = new ParsePosition(0);
                  Date result = parse(source, pos);
                  if (pos.index == 0)
                      throw new ParseException("Unparseable date: \"" + source + "\"" ,
                          pos.errorIndex);
                  return result;
              }
          • 2. Re: Date fromat parsing in java
            EJP
            Moderator action: Posting copyright code is contrary to the Code of Conduct for these forums.
            • 3. Re: Date fromat parsing in java
              980759
              i understood, here i just tell OP to understand what source code doing, :) Thanks EJP
              • 4. Re: Date fromat parsing in java
                EJP
                What you posted is not correct. It throws an exception if it wasn't able to advance the parse, not 'if you did not pass anything to parse'.
                • 5. Re: Date fromat parsing in java
                  980759
                  yes, not blank what i mean,
                  df.setLenient(false);