Joda DateTime - 一个月中的天数范围
Joda DateTime - range of days in month
我这样使用 Joda DateTime:
DateTime dateTime = new DateTime().withDayOfMonth(31).withMonthOfYear(10).withYear(2017);
并得到以下异常:
org.joda.time.IllegalFieldValueException: Value 0 for dayOfMonth must be in the range [1,30]
但我不知道为什么 - 十月总是有 31 天?
似乎 DateTime API 根据输入的月份检查有效日期。因此,如果您将月份放在首位,那么它应该可以正常工作。
DateTime dateTime = new DateTime().withMonthOfYear(10).withDayOfMonth(31).withYear(2017);
我这样使用 Joda DateTime:
DateTime dateTime = new DateTime().withDayOfMonth(31).withMonthOfYear(10).withYear(2017);
并得到以下异常:
org.joda.time.IllegalFieldValueException: Value 0 for dayOfMonth must be in the range [1,30]
但我不知道为什么 - 十月总是有 31 天?
似乎 DateTime API 根据输入的月份检查有效日期。因此,如果您将月份放在首位,那么它应该可以正常工作。
DateTime dateTime = new DateTime().withMonthOfYear(10).withDayOfMonth(31).withYear(2017);