简体   繁体   English

JodaTime整数最小/最大值解析

[英]JodaTime Integer Min/Max Value parsing

Can someone give me some information about this parsing behavior of JodaTime? 有人可以给我一些有关JodaTime解析行为的信息吗?

String minValueString = new DateTime(Long.MIN_VALUE).toString();
System.out.println(minValueString);
DateTime minDateTime = DateTime.parse(minValueString);
System.out.println(minDateTime.toString());

prints this, 打印此,

-292275055-05-16T17:40:32.192+00:53:28

but throws this Exception for the parsing part 但是为解析部分抛出此异常

org.joda.time.IllegalFieldValueException: Cannot parse "-292275055-05-16T17:40:32.192+00:53:28": Value -292275055 for year must be in the range [-292275054,292278993]

Java 8's JSR310 (based on JodaTime) has a DateTime.MIN_VALUE and MAX_VALUE and the behaviour outside this range is not supported (as @Magnilex pointed out). Java 8的JSR310(基于JodaTime)具有DateTime.MIN_VALUE和MAX_VALUE,并且不支持超出此范围的行为(如@Magnilex所指出的)。

LocalDateTime.of( ... ) LocalDateTime.of(...)

throws

DateTimeException - if the value of any field is out of range, or if the day-of-month is invalid for the month-year DateTimeException-如果任何字段的值超出范围,或者当月中的某天对月份-年份无效

I suggest you migration to JSR-310 if you can. 我建议您尽可能迁移到JSR-310。 It is built in to Java 8 and there is a backport for earlier version. 它内置于Java 8中,并且有一个较早版本的反向端口。

https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html

The problem with the example above is that the start of the year -292275054-01-01 is out side the range so it cannot store this value so it parse the rest of the date. 上面的示例的问题在于, -292275054-01-01的开始年份超出了范围,因此它无法存储该值,因此它将分析日期的其余部分。 It could do this in theory with special handling but since this is a date before the dinosaurs lived and less than MIN_VALUE, you shouldn't expect it to be valid. 从理论上讲,它可以通过特殊处理来完成此操作,但是由于这是恐龙存在之前的某个日期,并且小于MIN_VALUE,因此您不应期望它是有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM