JodaTime 允许无效日期
JodaTime allows invalid date
我预计这会引发异常,因为 2016 年 2 月 1 日不是星期五:
final DateTimeFormatter formatter = DateTimeFormat.forPattern("EEEE, d MMMM yyyy");
final DateTime date = formatter.parseDateTime("Friday, 1 February 2016");
System.out.println(formatter.print(date));
相反,它打印出 Friday, 5 February 2016
。我在这里错过了什么?
文档中有说明(http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormatter.html)
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.
我预计这会引发异常,因为 2016 年 2 月 1 日不是星期五:
final DateTimeFormatter formatter = DateTimeFormat.forPattern("EEEE, d MMMM yyyy");
final DateTime date = formatter.parseDateTime("Friday, 1 February 2016");
System.out.println(formatter.print(date));
相反,它打印出 Friday, 5 February 2016
。我在这里错过了什么?
文档中有说明(http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormatter.html)
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.