spajak
spajak

Reputation: 603

How to validate date string in java

How to test a valid date string, assuming acceptable format is "YYYY-MM-DD"? (Y year, M month, D day)

I've tried DateFormat.parse(date_string) but it parses things like "2012-542-86".

Upvotes: 3

Views: 1248

Answers (2)

Keppil
Keppil

Reputation: 46209

As default, the parsing is lenient, which means it uses heuristics to parse non-exact matches.

You need to turn this off using #setLenient(false) to make it reject all non-matching formats.

Upvotes: 2

adarshr
adarshr

Reputation: 62573

The format should be yyyy-MM-dd

Have a look at valid formats at SimpleDateFormat API documentation.

Upvotes: 2

Related Questions