• Post Reply Bookmark Topic Watch Topic
  • New Topic

Check for Valid Date  RSS feed

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to check if adate entered is valid. I tried using DateFormat.parse. But when I enter 10/39/05 it accepts it and parses it into 11/08/05.

DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String historyDate = dateField.getText();
try {
df.parse(historyDate);
} catch (ParseException pe) {
System.err.println("Invalid Date: " + dateField.getText());
return;
}

Any thoughts would be appreciated. Thanks, Lisa
 
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing specific about GUI in this question, so I'll move it to Java In General (Intermediate)
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that the ParseException will report the kind of error you are looking for. It only deals with parsing the String. It does not deal with the contents of what is parsed.
 
Sheriff
Posts: 23506
47
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try addingIt's not clear to me why "lenient" date parsing (the situation you described there) should even be an option, let alone why it should be the default option. The designers of Java seem to have been smoking some strange substances when they designed the parts dealing with dates.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!