[英]How to validate date string in java
How to test a valid date string, assuming acceptable format is "YYYY-MM-DD"? 假设可接受的格式是“YYYY-MM-DD”,如何测试有效的日期字符串? (Y year, M month, D day) (Y年,M月,D日)
I've tried DateFormat.parse(date_string)
but it parses things like "2012-542-86". 我已经尝试了DateFormat.parse(date_string)
但它解析了像“2012-542-86”这样的东西。
The format should be yyyy-MM-dd
格式应为yyyy-MM-dd
Have a look at valid formats at SimpleDateFormat
API documentation . 在SimpleDateFormat
API文档中查看有效格式。
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. 您需要使用#setLenient(false)将其关闭以使其拒绝所有不匹配的格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.