0

It was a sub-question in my last post, to which I didn't get the answer.. If the String represents the 31st of April, June, etc., the exception is not thrown, but everything else works fine (32nd of April or May for example throws an exception). What I am doing wrong?

public OrderDate(String date) throws IllegalDateFormatException
{   
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yy");
    try 
    {
        dateFormat.setLenient(false);
        dateFormat.parse(date);
        this.date = date;
    }
    catch(ParseException e) 
    {
        throw new IllegalDateFormatException("Date must have the following"
                                                     + " format: dd/mm/yy");
    }
}
ArthurV
  • 113
  • 2
  • 8
  • 2
    `mm` = minutes, you need to use `MM` = months. See also [What is the use of “lenient ”?](http://stackoverflow.com/questions/7606387/what-is-the-use-of-lenient) – Jesper Mar 29 '15 at 18:25
  • @Jesper But is the statement "dateFormat.parse(date);" appropriate here? – ArthurV Mar 29 '15 at 19:12
  • Not by itself, because you're not doing anything with the value returned by `parse`. – Jesper Mar 29 '15 at 19:56
  • @Jesper what would you suggest then to make it look right? – ArthurV Mar 29 '15 at 20:52
  • Store the date as a `java.util.Date` and not as a `String`, use the format string `dd/MM/yy` (`MM` = months, you do not want `mm` = minutes), store the result of `parse`: `this.date = dateFormat.parse(date);` – Jesper Mar 30 '15 at 06:57

0 Answers0