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

发布于 2024-10-07 04:45:26 字数 388 浏览 3 评论 0 原文

我有一个带有模式 yyyy-Md" 的 SimpleDateFormat,以及以下场景:

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

输出是 Sat Mar 04 00:00:00 EST 2

我的目标是只捕获日期格式如 2004-02-03 并忽略 02-03-04。我认为模式中的 yyyy 需要 4 位数的年份,但显然情况并非如此,任何人都可以解释为什么这不抛出一个。解析异常?

I have a SimpleDateFormat with the pattern yyyy-M-d", and the following scenario:

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

My goal was to only catch dates in the format like 2004-02-03 and to ignore 02-03-04. I thought the yyyy in the pattern would require a 4 digit year, but clearly this is not the case. Can anyone explain why this is not throwing a parse exception? I would like it to...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别忘他 2024-10-14 04:45:26

好吧,我可以从文档中解释它

对于解析,如果模式字母的数量超过 2,则无论位数多少,都会按字面解释年份。因此,使用模式“MM/dd/yyyy”,“01/11/12”解析为公元 12 年 1 月 11 日

可能 Joda Time 会更严格 - 一般来说,它是一个更好的 API,IMO...

如果解析后年份小于 1000,你总是可以抛出异常...

Well, I can explain it from the docs:

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.

It's possible that Joda Time would be stricter - and it's a better API in general, IMO...

You could always throw an exception if the year is less than 1000 after parsing...

凉栀 2024-10-14 04:45:26

tl;dr

使用 java.time,具有该格式模式的输入会失败,正如您所期望的那样。

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

...抛出 java.time.format.DateTimeParseException

java.time

您使用的类现在已被 JSR 310 中定义并内置于 Java 8 中的现代 java.time 类所取代以及稍后。

LocalDate 类表示仅日期值,没有时间,也没有 时区与 UTC 的偏移量

根据您的要求定义自定义格式模式。使用 DateTimeFormatter 类。

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

尝试解析您的输入。

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

我们遇到 DateTimeParseException,在输入年份缺少世纪时失败。正如你所期望的那样。

线程“main”中的异常 java.time.format.DateTimeParseException:无法在索引 0 处解析文本“02-03-04”


关于 java.time

java.time< /a> 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如java.util.Date, 日历, & SimpleDateFormat

要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310

Joda-Time 项目,现已在 维护模式,建议迁移到 java.time 类。

您可以直接与数据库交换java.time对象。使用符合 JDBC 驱动程序 /jeps/170" rel="nofollow noreferrer">JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* 类。

从哪里获取 java.time 类?

ThreeTen-Extra 项目通过附加类扩展了 java.time 。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如 间隔YearWeek<代码>YearQuarter,以及更多

tl;dr

Using java.time, that input with that formatting pattern fails, just as you expected.

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

…throws java.time.format.DateTimeParseException

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.

The LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.

Define a custom formatting pattern as you asked. Use the DateTimeFormatter class.

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. Just as you expected.

Exception in thread "main" java.time.format.DateTimeParseException: Text '02-03-04' could not be parsed at index 0


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文