gpt4 book ai didi

java - JodaTime 不会在不匹配的星期几和月份失败

转载 作者:行者123 更新时间:2023-11-29 08:45:04 25 4
gpt4 key购买 nike

在下面的代码中,我预计会抛出一个异常,因为星期几与月份的日期不匹配。我在 DateTimeFormat Javadocs 中找不到任何内容关于对此类不匹配“严格”的任何一种说法,但我很困惑,允许这样的错误数据通过。我是否误解了什么或者这是我应该以其他方式处理的有意识的设计决定?

public static void main(String[] args) {
DateTimeFormatter DATE_FORMAT_LONG_DATE = //
DateTimeFormat.forPattern("EEEE, d MMMM yyyy")//
.withZone(DateTimeZone.forID("Australia/Melbourne"));
final String text = "Friday, 1 February 2016"; // Wrong.
final DateTime parsed = DATE_FORMAT_LONG_DATE.parseDateTime(text);
System.out.println(text);
System.out.println(DATE_FORMAT_LONG_DATE.print(parsed));
}

和输出:

Friday, 1 February 2016
Friday, 5 February 2016

最佳答案

你是对的,确实如此 - 如 documented (强调我的):

Parsing builds up the resultant instant by 'setting' the value of each parsed field from largest to smallest onto an initial instant, typically 1970-01-01T00:00Z. This design means that day-of-month is set before day-of-week. As such, if both the day-of-month and day-of-week are parsed, and the day-of-week is incorrect, then the day-of-week overrides the day-of-month. This has a side effect if the input is not consistent.

我同意这很不幸 - 但您总是可以在之后重新格式化并检查值是否不同。

关于java - JodaTime 不会在不匹配的星期几和月份失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36966210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com