要编写测试,我需要将 LocalDateTime
设置为 0000-00-00 00:00:00
。
当我尝试使用格式化程序设置它时:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime.parse("0000-00-00 00:00:00", formatter)
它抛出异常:
java.time.format.DateTimeParseException: Text '0000-00-00 00:00:00' could not be parsed: Invalid value for YearOfEra (valid values 1 - 999999999/1000000000): 0
我该怎么做?
最佳答案
您正在尝试设置 0 年 0 月的第 0 天。因此存在 3 个错误:
- 任何月份都没有第 0 天。日期总是从 1 开始
- 任何年份都没有 0 月。月份总是从 1 开始
- There's no year 0 in the Gregorian calendar 。这就是您收到的错误(
YearOfEra 的值无效
)。您应该设置大于 0 的年份
关于java - 如何将时间 "0000-00-00 00:00:00"设置为 LocalDateTime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59800273/