0
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(df.parse("32/12/2016"));
System.out.println(df.parse("30/02/2017"));
System.out.println(df.parse("31/11/2016"));

I'm expecting all the above scenarios as ParseException but but I'm getting output as below:

Sun Jan 01 00:00:00 GMT 2017

Thu Mar 02 00:00:00 GMT 2017

Thu Dec 01 00:00:00 GMT 2016

In my case all the above scenarios are validation failed one.

Note: I can't do equals(post and pre parse the date.) also why because my date inputs may come as DD/MM/YYYY or D/M/YY.

Is there any other way to do this?

Community
  • 1
  • 1
Rajesh Narravula
  • 1,433
  • 3
  • 26
  • 54
  • May I ask what is the situation in your app where you would normally be expecting a date such as February 30? One alternative might be to use Java 8's date API. – Tim Biegeleisen Mar 16 '17 at 07:01
  • If user send date by mistake i need to send response back saying invalid date. so he can send 28/29 of feb month date. – Rajesh Narravula Mar 16 '17 at 07:02
  • 1
    Try to add this line df.setLenient(false); it throws exception exception after adding this line. Without this it adds the days to next cycle. – Prasad Mar 16 '17 at 07:08

1 Answers1

1

You can call df.setLenient(false); to make it verify that the dates are valid.

zsmb13
  • 85,752
  • 11
  • 221
  • 226