简体   繁体   English

尽管格式化模式中为“ yyyy”,但SimpleDateFormat无法拒绝输入年份缺少的世纪输入

[英]SimpleDateFormat fails to reject input missing century on the year of the input, despite “yyyy” in the formatting pattern

I have a SimpleDateFormat with the pattern yyyy-Md" , and the following scenario: 我有一个模式为yyyy-Md"的SimpleDateFormat,并且具有以下情形:

String str = "02-03-04";        
SimpleDateFormat f = new SimpleDateFormat("yyyy-M-d");
f.setLenient(false);
System.out.println(f.parse(str));

The output is Sat Mar 04 00:00:00 EST 2 输出为Sat Mar 04 00:00:00 EST 2

My goal was to only catch dates in the format like 2004-02-03 and to ignore 02-03-04. 我的目标是只捕获格式为2004-02-03的日期,而忽略02-03-04。 I thought the yyyy in the pattern would require a 4 digit year, but clearly this is not the case. 我认为格式中的yyyy需要4位数字的年份,但显然不是这样。 Can anyone explain why this is not throwing a parse exception? 谁能解释为什么这没有引发解析异常? I would like it to... 我想要...

Well, I can explain it from the docs : 好吧,我可以从docs中解释一下:

For parsing, if the number of pattern letters is more than 2, the year is interpreted literally, regardless of the number of digits. 对于解析,如果模式字母的数量大于2,则将按字面意义解释年份,而不考虑数字的数量。 So using the pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 AD 因此,使用模式“ MM / dd / yyyy”,“ 01/11/12”将解析为公元12月11日

It's possible that Joda Time would be stricter - and it's a better API in general, IMO... Joda Time可能会更严格-IMO总体而言是更好的API ...

You could always throw an exception if the year is less than 1000 after parsing... 如果解析后的年份少于1000年,您总是可以抛出异常。

tl;dr tl; dr

Using java.time , that input with that formatting pattern fails, just as you expected. 如您所料,使用java.time时 ,使用该格式模式的输入失败。

LocalDate
.parse(
    "02-03-04" ,
    DateTimeFormatter.ofPattern ( "uuuu-M-d" )
)

…throws java.time.format.DateTimeParseException …引发java.time.format.DateTimeParseException

java.time java.time

The classes you were using are now supplanted by the modern java.time classes defined in JSR 310 and built into Java 8 and later. 现在,您所使用的类已由 JSR 310中定义并内置于Java 8及更高版本中的现代java.time类所取代。

The LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC . LocalDate类表示仅包含日期的值,没有日期, 时间段offset-from-UTC

Define a custom formatting pattern as you asked. 根据要求定义自定义格式设置模式。 Use the DateTimeFormatter class. 使用DateTimeFormatter类。

DateTimeFormatter f = DateTimeFormatter.ofPattern ( "uuuu-M-d" );

Try to parse your input. 尝试解析您的输入。

String input = "02-03-04";
LocalDate localDate = LocalDate.parse ( input , f );

We encounter a DateTimeParseException , failing on the missing century of the year of the input. 我们遇到了DateTimeParseException ,在缺少输入年份的世纪失败。 Just as you expected. 如您所料。

Exception in thread "main" java.time.format.DateTimeParseException: Text '02-03-04' could not be parsed at index 0 线程“主”中的异常java.time.format.DateTimeParseException:无法在索引0处解析文本“ 02-03-04”


About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧的旧式日期时间类,例如java.util.DateCalendarSimpleDateFormat

To learn more, see the Oracle Tutorial . 要了解更多信息,请参见Oracle教程 And search Stack Overflow for many examples and explanations. 并在Stack Overflow中搜索许多示例和说明。 Specification is JSR 310 . 规格为JSR 310

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

You may exchange java.time objects directly with your database. 您可以直接与数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later. 使用与JDBC 4.2或更高版本兼容的JDBC驱动程序 No need for strings, no need for java.sql.* classes. 不需要字符串,不需要java.sql.*类。

Where to obtain the java.time classes? 在哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目为将来可能在java.time中添加内容提供了一个试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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