简体   繁体   中英

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".

The format should be yyyy-MM-dd

Have a look at valid formats at SimpleDateFormat API documentation .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM