日期格式验证不适用于 yyyy-MM-dd
Date Format validation not working for yyyy-MM-dd
我想验证格式为 yyyy-MM-dd 的日期。
如果我给出两位数的年份(即 YY 而不是 YYYY),它不会抛出任何异常,并且 00 会在解析日期时附加到日期时间格式。
我已经添加了 setLenient(false);
,但它仍然没有正确验证。
谁能帮我解决这个问题?
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
formatter.setLenient(false);
try {
Date date = (Date)formatter.parse("15-05-30"); //In this line year is getting appened with 00 and becomes 0015
reciptDate = dateFormat.format(date);
} catch (ParseException pe) {
return false;
}
API docs for SimpleDateFormat
指定,对于年:
For parsing, if the number of pattern letters is more than 2, the year is interpreted literally, regardless of the number of digits. So using the pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D.
因此,您不能使用SimpleDateFormat
as-is来执行您想要的验证(请注意,1、2 或 3 位数字的年份是有效年份,> 4 位数字的年份也是如此,但我认为这超出了问题的范围)。
使用正则表达式验证年份是否正好是 4 位数字应该是微不足道的。
例如:
Pattern pattern = Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}");
System.out.println("15-05-30: " + pattern.matcher("15-05-30").matches());
System.out.println("2015-05-30: " + pattern.matcher("2015-05-30").matches());
System.out.println("0015-05-30: " + pattern.matcher("0015-05-30").matches());
输出:
15-05-30: false
2015-05-30: true
0015-05-30: true
如果您在 Java-8 上工作,您可以指定
年份组件的最小宽度。
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
.appendValue(ChronoField.YEAR, 4, 4, SignStyle.NEVER)
.appendPattern("-MM-dd")
.toFormatter();
LocalDate date = LocalDate.parse("15-05-30", fmt);
错误信息是:
Exception in thread "main" java.time.format.DateTimeParseException:
Text '15-05-30' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:400)
我想验证格式为 yyyy-MM-dd 的日期。 如果我给出两位数的年份(即 YY 而不是 YYYY),它不会抛出任何异常,并且 00 会在解析日期时附加到日期时间格式。
我已经添加了 setLenient(false);
,但它仍然没有正确验证。
谁能帮我解决这个问题?
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
formatter.setLenient(false);
try {
Date date = (Date)formatter.parse("15-05-30"); //In this line year is getting appened with 00 and becomes 0015
reciptDate = dateFormat.format(date);
} catch (ParseException pe) {
return false;
}
API docs for SimpleDateFormat
指定,对于年:
For parsing, if the number of pattern letters is more than 2, the year is interpreted literally, regardless of the number of digits. So using the pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D.
因此,您不能使用SimpleDateFormat
as-is来执行您想要的验证(请注意,1、2 或 3 位数字的年份是有效年份,> 4 位数字的年份也是如此,但我认为这超出了问题的范围)。
使用正则表达式验证年份是否正好是 4 位数字应该是微不足道的。
例如:
Pattern pattern = Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}");
System.out.println("15-05-30: " + pattern.matcher("15-05-30").matches());
System.out.println("2015-05-30: " + pattern.matcher("2015-05-30").matches());
System.out.println("0015-05-30: " + pattern.matcher("0015-05-30").matches());
输出:
15-05-30: false
2015-05-30: true
0015-05-30: true
如果您在 Java-8 上工作,您可以指定 年份组件的最小宽度。
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
.appendValue(ChronoField.YEAR, 4, 4, SignStyle.NEVER)
.appendPattern("-MM-dd")
.toFormatter();
LocalDate date = LocalDate.parse("15-05-30", fmt);
错误信息是:
Exception in thread "main" java.time.format.DateTimeParseException:
Text '15-05-30' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:400)