SimpleDateFormat parse(string str) doesn't throw an exception when str = 2011/12/12aaaaaaaaa?











up vote
9
down vote

favorite
2












Here is an example:



public MyDate() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
sdf.setLenient(false);
String t1 = "2011/12/12aaa";
System.out.println(sdf.parse(t1));
}


2011/12/12aaa is not a valid date string. However the function prints "Mon Dec 12 00:00:00 PST 2011" and ParseException isn't thrown.



Can anyone tell me how to let SimpleDateFormat treat "2011/12/12aaa" as an invalid date string and throw an exception?










share|improve this question




























    up vote
    9
    down vote

    favorite
    2












    Here is an example:



    public MyDate() throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
    sdf.setLenient(false);
    String t1 = "2011/12/12aaa";
    System.out.println(sdf.parse(t1));
    }


    2011/12/12aaa is not a valid date string. However the function prints "Mon Dec 12 00:00:00 PST 2011" and ParseException isn't thrown.



    Can anyone tell me how to let SimpleDateFormat treat "2011/12/12aaa" as an invalid date string and throw an exception?










    share|improve this question


























      up vote
      9
      down vote

      favorite
      2









      up vote
      9
      down vote

      favorite
      2






      2





      Here is an example:



      public MyDate() throws ParseException {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
      sdf.setLenient(false);
      String t1 = "2011/12/12aaa";
      System.out.println(sdf.parse(t1));
      }


      2011/12/12aaa is not a valid date string. However the function prints "Mon Dec 12 00:00:00 PST 2011" and ParseException isn't thrown.



      Can anyone tell me how to let SimpleDateFormat treat "2011/12/12aaa" as an invalid date string and throw an exception?










      share|improve this question















      Here is an example:



      public MyDate() throws ParseException {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
      sdf.setLenient(false);
      String t1 = "2011/12/12aaa";
      System.out.println(sdf.parse(t1));
      }


      2011/12/12aaa is not a valid date string. However the function prints "Mon Dec 12 00:00:00 PST 2011" and ParseException isn't thrown.



      Can anyone tell me how to let SimpleDateFormat treat "2011/12/12aaa" as an invalid date string and throw an exception?







      java simpledateformat parseexception






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 9 '16 at 11:10









      eis

      34.2k894141




      34.2k894141










      asked Dec 8 '11 at 8:44









      Terminal User

      3981917




      3981917
























          6 Answers
          6






          active

          oldest

          votes

















          up vote
          12
          down vote



          accepted










          The JavaDoc on parse(...) states the following:




          parsing does not necessarily use all characters up to the end of the string




          It seems like you can't make SimpleDateFormat throw an exception, but you can do the following:



          SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
          sdf.setLenient(false);
          ParsePosition p = new ParsePosition( 0 );
          String t1 = "2011/12/12aaa";
          System.out.println(sdf.parse(t1,p));

          if(p.getIndex() < t1.length()) {
          throw new ParseException( t1, p.getIndex() );
          }


          Basically, you check whether the parse consumed the entire string and if not you have invalid input.






          share|improve this answer






























            up vote
            4
            down vote













            To chack whether a date is valid
            The following method returns if the date is in valid otherwise it will return false.



            public boolean isValidDate(String date) {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
            Date testDate = null;
            try {
            testDate = sdf.parse(date);
            }
            catch (ParseException e) {
            return false;
            }
            if (!sdf.format(testDate).equals(date)) {
            return false;
            }
            return true;

            }


            Have a look on the following class which can check whether the date is valid or not



            ** Sample Example**



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

            public class DateValidCheck {


            public static void main(String args) {

            if(new DateValidCheck().isValidDate("2011/12/12aaa")){
            System.out.println("...date is valid");
            }else{
            System.out.println("...date is invalid...");
            }

            }


            public boolean isValidDate(String date) {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
            Date testDate = null;
            try {
            testDate = sdf.parse(date);
            }
            catch (ParseException e) {
            return false;
            }
            if (!sdf.format(testDate).equals(date)) {
            return false;
            }
            return true;

            }

            }





            share|improve this answer




























              up vote
              2
              down vote













              After it successfully parsed the entire pattern string SimpleDateFormat stops evaluating the data it was given to parse.






              share|improve this answer

















              • 1




                agree... too bad
                – Terminal User
                Dec 8 '11 at 9:05


















              up vote
              1
              down vote













              Java 8 LocalDate may be used:



              public static boolean isDate(String date) {
              try {
              LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy/MM/dd"));
              return true;
              } catch (DateTimeParseException e) {
              return false;
              }
              }


              If input argument is "2011/12/12aaaaaaaaa", output is false;



              If input argument is "2011/12/12", output is true






              share|improve this answer




























                up vote
                0
                down vote













                Take a look on the method documentation which says: ParseException if the beginning of the specified string cannot be parsed.



                Method source code with javadoc:



                /**
                * 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;
                }





                share|improve this answer




























                  up vote
                  0
                  down vote













                  You can use the ParsePosition class or the sdf.setLenient(false) function



                  Docs:
                  http://docs.oracle.com/javase/7/docs/api/java/text/ParsePosition.html
                  http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#setLenient(boolean)






                  share|improve this answer



















                  • 2




                    I think parse method only care about the start pos, not the end pos. isLenient doesn't work
                    – Terminal User
                    Dec 8 '11 at 8:57






                  • 1




                    When linking JavaDocs, please try to link the current version (which is 7, not 1.4.2).
                    – Thomas
                    Dec 8 '11 at 9:01










                  • @TerminalUser mm I tried it myself with an example but setLeniet() doesn't seems to work and ParsePosition only works when there is actually a exception thrown. You can check if both Strings have the same length after parseing without an Exception and then throw an exception manual. Not fully what you request but...
                    – Michel
                    Dec 8 '11 at 9:08











                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8428313%2fsimpledateformat-parsestring-str-doesnt-throw-an-exception-when-str-2011-12%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  6 Answers
                  6






                  active

                  oldest

                  votes








                  6 Answers
                  6






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  12
                  down vote



                  accepted










                  The JavaDoc on parse(...) states the following:




                  parsing does not necessarily use all characters up to the end of the string




                  It seems like you can't make SimpleDateFormat throw an exception, but you can do the following:



                  SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
                  sdf.setLenient(false);
                  ParsePosition p = new ParsePosition( 0 );
                  String t1 = "2011/12/12aaa";
                  System.out.println(sdf.parse(t1,p));

                  if(p.getIndex() < t1.length()) {
                  throw new ParseException( t1, p.getIndex() );
                  }


                  Basically, you check whether the parse consumed the entire string and if not you have invalid input.






                  share|improve this answer



























                    up vote
                    12
                    down vote



                    accepted










                    The JavaDoc on parse(...) states the following:




                    parsing does not necessarily use all characters up to the end of the string




                    It seems like you can't make SimpleDateFormat throw an exception, but you can do the following:



                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
                    sdf.setLenient(false);
                    ParsePosition p = new ParsePosition( 0 );
                    String t1 = "2011/12/12aaa";
                    System.out.println(sdf.parse(t1,p));

                    if(p.getIndex() < t1.length()) {
                    throw new ParseException( t1, p.getIndex() );
                    }


                    Basically, you check whether the parse consumed the entire string and if not you have invalid input.






                    share|improve this answer

























                      up vote
                      12
                      down vote



                      accepted







                      up vote
                      12
                      down vote



                      accepted






                      The JavaDoc on parse(...) states the following:




                      parsing does not necessarily use all characters up to the end of the string




                      It seems like you can't make SimpleDateFormat throw an exception, but you can do the following:



                      SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
                      sdf.setLenient(false);
                      ParsePosition p = new ParsePosition( 0 );
                      String t1 = "2011/12/12aaa";
                      System.out.println(sdf.parse(t1,p));

                      if(p.getIndex() < t1.length()) {
                      throw new ParseException( t1, p.getIndex() );
                      }


                      Basically, you check whether the parse consumed the entire string and if not you have invalid input.






                      share|improve this answer














                      The JavaDoc on parse(...) states the following:




                      parsing does not necessarily use all characters up to the end of the string




                      It seems like you can't make SimpleDateFormat throw an exception, but you can do the following:



                      SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
                      sdf.setLenient(false);
                      ParsePosition p = new ParsePosition( 0 );
                      String t1 = "2011/12/12aaa";
                      System.out.println(sdf.parse(t1,p));

                      if(p.getIndex() < t1.length()) {
                      throw new ParseException( t1, p.getIndex() );
                      }


                      Basically, you check whether the parse consumed the entire string and if not you have invalid input.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 8 '11 at 8:59

























                      answered Dec 8 '11 at 8:48









                      Thomas

                      69k989124




                      69k989124
























                          up vote
                          4
                          down vote













                          To chack whether a date is valid
                          The following method returns if the date is in valid otherwise it will return false.



                          public boolean isValidDate(String date) {

                          SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
                          Date testDate = null;
                          try {
                          testDate = sdf.parse(date);
                          }
                          catch (ParseException e) {
                          return false;
                          }
                          if (!sdf.format(testDate).equals(date)) {
                          return false;
                          }
                          return true;

                          }


                          Have a look on the following class which can check whether the date is valid or not



                          ** Sample Example**



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

                          public class DateValidCheck {


                          public static void main(String args) {

                          if(new DateValidCheck().isValidDate("2011/12/12aaa")){
                          System.out.println("...date is valid");
                          }else{
                          System.out.println("...date is invalid...");
                          }

                          }


                          public boolean isValidDate(String date) {

                          SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
                          Date testDate = null;
                          try {
                          testDate = sdf.parse(date);
                          }
                          catch (ParseException e) {
                          return false;
                          }
                          if (!sdf.format(testDate).equals(date)) {
                          return false;
                          }
                          return true;

                          }

                          }





                          share|improve this answer

























                            up vote
                            4
                            down vote













                            To chack whether a date is valid
                            The following method returns if the date is in valid otherwise it will return false.



                            public boolean isValidDate(String date) {

                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
                            Date testDate = null;
                            try {
                            testDate = sdf.parse(date);
                            }
                            catch (ParseException e) {
                            return false;
                            }
                            if (!sdf.format(testDate).equals(date)) {
                            return false;
                            }
                            return true;

                            }


                            Have a look on the following class which can check whether the date is valid or not



                            ** Sample Example**



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

                            public class DateValidCheck {


                            public static void main(String args) {

                            if(new DateValidCheck().isValidDate("2011/12/12aaa")){
                            System.out.println("...date is valid");
                            }else{
                            System.out.println("...date is invalid...");
                            }

                            }


                            public boolean isValidDate(String date) {

                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
                            Date testDate = null;
                            try {
                            testDate = sdf.parse(date);
                            }
                            catch (ParseException e) {
                            return false;
                            }
                            if (!sdf.format(testDate).equals(date)) {
                            return false;
                            }
                            return true;

                            }

                            }





                            share|improve this answer























                              up vote
                              4
                              down vote










                              up vote
                              4
                              down vote









                              To chack whether a date is valid
                              The following method returns if the date is in valid otherwise it will return false.



                              public boolean isValidDate(String date) {

                              SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
                              Date testDate = null;
                              try {
                              testDate = sdf.parse(date);
                              }
                              catch (ParseException e) {
                              return false;
                              }
                              if (!sdf.format(testDate).equals(date)) {
                              return false;
                              }
                              return true;

                              }


                              Have a look on the following class which can check whether the date is valid or not



                              ** Sample Example**



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

                              public class DateValidCheck {


                              public static void main(String args) {

                              if(new DateValidCheck().isValidDate("2011/12/12aaa")){
                              System.out.println("...date is valid");
                              }else{
                              System.out.println("...date is invalid...");
                              }

                              }


                              public boolean isValidDate(String date) {

                              SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
                              Date testDate = null;
                              try {
                              testDate = sdf.parse(date);
                              }
                              catch (ParseException e) {
                              return false;
                              }
                              if (!sdf.format(testDate).equals(date)) {
                              return false;
                              }
                              return true;

                              }

                              }





                              share|improve this answer












                              To chack whether a date is valid
                              The following method returns if the date is in valid otherwise it will return false.



                              public boolean isValidDate(String date) {

                              SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
                              Date testDate = null;
                              try {
                              testDate = sdf.parse(date);
                              }
                              catch (ParseException e) {
                              return false;
                              }
                              if (!sdf.format(testDate).equals(date)) {
                              return false;
                              }
                              return true;

                              }


                              Have a look on the following class which can check whether the date is valid or not



                              ** Sample Example**



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

                              public class DateValidCheck {


                              public static void main(String args) {

                              if(new DateValidCheck().isValidDate("2011/12/12aaa")){
                              System.out.println("...date is valid");
                              }else{
                              System.out.println("...date is invalid...");
                              }

                              }


                              public boolean isValidDate(String date) {

                              SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
                              Date testDate = null;
                              try {
                              testDate = sdf.parse(date);
                              }
                              catch (ParseException e) {
                              return false;
                              }
                              if (!sdf.format(testDate).equals(date)) {
                              return false;
                              }
                              return true;

                              }

                              }






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Dec 8 '11 at 9:46









                              Sunil Kumar Sahoo

                              36.8k45162232




                              36.8k45162232






















                                  up vote
                                  2
                                  down vote













                                  After it successfully parsed the entire pattern string SimpleDateFormat stops evaluating the data it was given to parse.






                                  share|improve this answer

















                                  • 1




                                    agree... too bad
                                    – Terminal User
                                    Dec 8 '11 at 9:05















                                  up vote
                                  2
                                  down vote













                                  After it successfully parsed the entire pattern string SimpleDateFormat stops evaluating the data it was given to parse.






                                  share|improve this answer

















                                  • 1




                                    agree... too bad
                                    – Terminal User
                                    Dec 8 '11 at 9:05













                                  up vote
                                  2
                                  down vote










                                  up vote
                                  2
                                  down vote









                                  After it successfully parsed the entire pattern string SimpleDateFormat stops evaluating the data it was given to parse.






                                  share|improve this answer












                                  After it successfully parsed the entire pattern string SimpleDateFormat stops evaluating the data it was given to parse.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Dec 8 '11 at 9:03









                                  DerMike

                                  8,505113956




                                  8,505113956








                                  • 1




                                    agree... too bad
                                    – Terminal User
                                    Dec 8 '11 at 9:05














                                  • 1




                                    agree... too bad
                                    – Terminal User
                                    Dec 8 '11 at 9:05








                                  1




                                  1




                                  agree... too bad
                                  – Terminal User
                                  Dec 8 '11 at 9:05




                                  agree... too bad
                                  – Terminal User
                                  Dec 8 '11 at 9:05










                                  up vote
                                  1
                                  down vote













                                  Java 8 LocalDate may be used:



                                  public static boolean isDate(String date) {
                                  try {
                                  LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy/MM/dd"));
                                  return true;
                                  } catch (DateTimeParseException e) {
                                  return false;
                                  }
                                  }


                                  If input argument is "2011/12/12aaaaaaaaa", output is false;



                                  If input argument is "2011/12/12", output is true






                                  share|improve this answer

























                                    up vote
                                    1
                                    down vote













                                    Java 8 LocalDate may be used:



                                    public static boolean isDate(String date) {
                                    try {
                                    LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy/MM/dd"));
                                    return true;
                                    } catch (DateTimeParseException e) {
                                    return false;
                                    }
                                    }


                                    If input argument is "2011/12/12aaaaaaaaa", output is false;



                                    If input argument is "2011/12/12", output is true






                                    share|improve this answer























                                      up vote
                                      1
                                      down vote










                                      up vote
                                      1
                                      down vote









                                      Java 8 LocalDate may be used:



                                      public static boolean isDate(String date) {
                                      try {
                                      LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy/MM/dd"));
                                      return true;
                                      } catch (DateTimeParseException e) {
                                      return false;
                                      }
                                      }


                                      If input argument is "2011/12/12aaaaaaaaa", output is false;



                                      If input argument is "2011/12/12", output is true






                                      share|improve this answer












                                      Java 8 LocalDate may be used:



                                      public static boolean isDate(String date) {
                                      try {
                                      LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy/MM/dd"));
                                      return true;
                                      } catch (DateTimeParseException e) {
                                      return false;
                                      }
                                      }


                                      If input argument is "2011/12/12aaaaaaaaa", output is false;



                                      If input argument is "2011/12/12", output is true







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered May 16 '17 at 8:26









                                      David

                                      1,3441620




                                      1,3441620






















                                          up vote
                                          0
                                          down vote













                                          Take a look on the method documentation which says: ParseException if the beginning of the specified string cannot be parsed.



                                          Method source code with javadoc:



                                          /**
                                          * 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;
                                          }





                                          share|improve this answer

























                                            up vote
                                            0
                                            down vote













                                            Take a look on the method documentation which says: ParseException if the beginning of the specified string cannot be parsed.



                                            Method source code with javadoc:



                                            /**
                                            * 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;
                                            }





                                            share|improve this answer























                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                              Take a look on the method documentation which says: ParseException if the beginning of the specified string cannot be parsed.



                                              Method source code with javadoc:



                                              /**
                                              * 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;
                                              }





                                              share|improve this answer












                                              Take a look on the method documentation which says: ParseException if the beginning of the specified string cannot be parsed.



                                              Method source code with javadoc:



                                              /**
                                              * 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;
                                              }






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Dec 8 '11 at 8:58









                                              amra

                                              10.6k53942




                                              10.6k53942






















                                                  up vote
                                                  0
                                                  down vote













                                                  You can use the ParsePosition class or the sdf.setLenient(false) function



                                                  Docs:
                                                  http://docs.oracle.com/javase/7/docs/api/java/text/ParsePosition.html
                                                  http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#setLenient(boolean)






                                                  share|improve this answer



















                                                  • 2




                                                    I think parse method only care about the start pos, not the end pos. isLenient doesn't work
                                                    – Terminal User
                                                    Dec 8 '11 at 8:57






                                                  • 1




                                                    When linking JavaDocs, please try to link the current version (which is 7, not 1.4.2).
                                                    – Thomas
                                                    Dec 8 '11 at 9:01










                                                  • @TerminalUser mm I tried it myself with an example but setLeniet() doesn't seems to work and ParsePosition only works when there is actually a exception thrown. You can check if both Strings have the same length after parseing without an Exception and then throw an exception manual. Not fully what you request but...
                                                    – Michel
                                                    Dec 8 '11 at 9:08















                                                  up vote
                                                  0
                                                  down vote













                                                  You can use the ParsePosition class or the sdf.setLenient(false) function



                                                  Docs:
                                                  http://docs.oracle.com/javase/7/docs/api/java/text/ParsePosition.html
                                                  http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#setLenient(boolean)






                                                  share|improve this answer



















                                                  • 2




                                                    I think parse method only care about the start pos, not the end pos. isLenient doesn't work
                                                    – Terminal User
                                                    Dec 8 '11 at 8:57






                                                  • 1




                                                    When linking JavaDocs, please try to link the current version (which is 7, not 1.4.2).
                                                    – Thomas
                                                    Dec 8 '11 at 9:01










                                                  • @TerminalUser mm I tried it myself with an example but setLeniet() doesn't seems to work and ParsePosition only works when there is actually a exception thrown. You can check if both Strings have the same length after parseing without an Exception and then throw an exception manual. Not fully what you request but...
                                                    – Michel
                                                    Dec 8 '11 at 9:08













                                                  up vote
                                                  0
                                                  down vote










                                                  up vote
                                                  0
                                                  down vote









                                                  You can use the ParsePosition class or the sdf.setLenient(false) function



                                                  Docs:
                                                  http://docs.oracle.com/javase/7/docs/api/java/text/ParsePosition.html
                                                  http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#setLenient(boolean)






                                                  share|improve this answer














                                                  You can use the ParsePosition class or the sdf.setLenient(false) function



                                                  Docs:
                                                  http://docs.oracle.com/javase/7/docs/api/java/text/ParsePosition.html
                                                  http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#setLenient(boolean)







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Dec 8 '11 at 9:04

























                                                  answered Dec 8 '11 at 8:53









                                                  Michel

                                                  4,678103651




                                                  4,678103651








                                                  • 2




                                                    I think parse method only care about the start pos, not the end pos. isLenient doesn't work
                                                    – Terminal User
                                                    Dec 8 '11 at 8:57






                                                  • 1




                                                    When linking JavaDocs, please try to link the current version (which is 7, not 1.4.2).
                                                    – Thomas
                                                    Dec 8 '11 at 9:01










                                                  • @TerminalUser mm I tried it myself with an example but setLeniet() doesn't seems to work and ParsePosition only works when there is actually a exception thrown. You can check if both Strings have the same length after parseing without an Exception and then throw an exception manual. Not fully what you request but...
                                                    – Michel
                                                    Dec 8 '11 at 9:08














                                                  • 2




                                                    I think parse method only care about the start pos, not the end pos. isLenient doesn't work
                                                    – Terminal User
                                                    Dec 8 '11 at 8:57






                                                  • 1




                                                    When linking JavaDocs, please try to link the current version (which is 7, not 1.4.2).
                                                    – Thomas
                                                    Dec 8 '11 at 9:01










                                                  • @TerminalUser mm I tried it myself with an example but setLeniet() doesn't seems to work and ParsePosition only works when there is actually a exception thrown. You can check if both Strings have the same length after parseing without an Exception and then throw an exception manual. Not fully what you request but...
                                                    – Michel
                                                    Dec 8 '11 at 9:08








                                                  2




                                                  2




                                                  I think parse method only care about the start pos, not the end pos. isLenient doesn't work
                                                  – Terminal User
                                                  Dec 8 '11 at 8:57




                                                  I think parse method only care about the start pos, not the end pos. isLenient doesn't work
                                                  – Terminal User
                                                  Dec 8 '11 at 8:57




                                                  1




                                                  1




                                                  When linking JavaDocs, please try to link the current version (which is 7, not 1.4.2).
                                                  – Thomas
                                                  Dec 8 '11 at 9:01




                                                  When linking JavaDocs, please try to link the current version (which is 7, not 1.4.2).
                                                  – Thomas
                                                  Dec 8 '11 at 9:01












                                                  @TerminalUser mm I tried it myself with an example but setLeniet() doesn't seems to work and ParsePosition only works when there is actually a exception thrown. You can check if both Strings have the same length after parseing without an Exception and then throw an exception manual. Not fully what you request but...
                                                  – Michel
                                                  Dec 8 '11 at 9:08




                                                  @TerminalUser mm I tried it myself with an example but setLeniet() doesn't seems to work and ParsePosition only works when there is actually a exception thrown. You can check if both Strings have the same length after parseing without an Exception and then throw an exception manual. Not fully what you request but...
                                                  – Michel
                                                  Dec 8 '11 at 9:08


















                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8428313%2fsimpledateformat-parsestring-str-doesnt-throw-an-exception-when-str-2011-12%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Wiesbaden

                                                  Marschland

                                                  Dieringhausen