tl;dr
OffsetDateTime.parse( "2015-07-35T09:32:05.543+02:00" )
… catch ( DateTimeParseException e )
java.time
麻烦的旧日期时间类,例如java.util.Date https://docs.oracle.com/javase/9/docs/api/java/util/Date.html, java.util.Calendar https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html, and java.text.SimpleDateFormat
现在legacy https://en.wikipedia.org/wiki/Legacy_system,被取代java.time https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.htmlJava 8 和 Java 9 中内置的类。
同样,乔达时间 http://www.joda.org/joda-time/项目现已在维护模式 https://en.wikipedia.org/wiki/Maintenance_mode,团队建议迁移到java.time http://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html类。
OffsetDateTime
& DateTimeParseException
要确定无效的日期时间字符串,请尝试解析并捕获异常。鉴于您的输入具有相对于 UTC 的偏移量,但没有时区,请解析为OffsetDateTime https://docs.oracle.com/javase/9/docs/api/java/time/OffsetDateTime.html对象。无效输入抛出DateTimeParseException https://docs.oracle.com/javase/9/docs/api/java/time/format/DateTimeParseException.html.
String inputGood = "2015-07-30T09:32:05.543+02:00" ;
String inputBad = "2015-07-35T09:32:05.543+02:00" ;
try{
// Good
OffsetDateTime odtGood = OffsetDateTime.parse( inputGood ) ;
System.out.println( "odtGood.toString(): " + odtGood ) ;
// Bad
OffsetDateTime odtBad = OffsetDateTime.parse( inputBad ) ;
System.out.println( "odtBad.toString(): " + odtBad ) ;
} catch ( DateTimeParseException e ) {
System.out.println( e ) ;
}
See 代码在 IdeOne.com 上实时运行 https://ideone.com/DYNUUF.
odtGood.toString(): 2015-07-30T09:32:05.543+02:00
java.time.format.DateTimeParseException:无法解析文本'2015-07-35T09:32:05.543 + 02:00':DayOfMonth的值无效(有效值1 - 28/31):35
关于 java.time
The java.time http://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html框架内置于 Java 8 及更高版本中。这些课程取代了麻烦的旧课程legacy https://en.wikipedia.org/wiki/Legacy_system日期时间类,例如java.util.Date https://docs.oracle.com/javase/9/docs/api/java/util/Date.html, Calendar https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html, & SimpleDateFormat http://docs.oracle.com/javase/9/docs/api/java/text/SimpleDateFormat.html.
The 乔达时间 http://www.joda.org/joda-time/项目,现在在维护模式 https://en.wikipedia.org/wiki/Maintenance_mode,建议迁移到java.time http://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html类。
要了解更多信息,请参阅甲骨文教程 http://docs.oracle.com/javase/tutorial/datetime/TOC.html。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310 https://jcp.org/en/jsr/detail?id=310.
从哪里获取 java.time 类?
-
Java SE 8 https://en.wikipedia.org/wiki/Java_version_history#Java_SE_8, Java SE 9 https://en.wikipedia.org/wiki/Java_version_history#Java_SE_9, and later
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
-
Java SE 6 https://en.wikipedia.org/wiki/Java_version_history#Java_SE_6 and Java SE 7 https://en.wikipedia.org/wiki/Java_version_history#Java_SE_7
- 许多 java.time 功能都向后移植到 Java 6 和 7三十后端口 http://www.threeten.org/threetenbp/.
-
Android https://en.wikipedia.org/wiki/Android_(operating_system)
- 更高版本的 Android 捆绑了 java.time 类的实现。
- 对于早期的 Android,三十ABP https://github.com/JakeWharton/ThreeTenABP项目适应三十后端口(上文提到的)。看如何使用 ThreeTenABP... http://stackoverflow.com/q/38922754/642706.
The 三十额外 http://www.threeten.org/threeten-extra/项目通过附加类扩展了 java.time。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如Interval http://www.threeten.org/threeten-extra/apidocs/org/threeten/extra/Interval.html, YearWeek http://www.threeten.org/threeten-extra/apidocs/org/threeten/extra/YearWeek.html, YearQuarter http://www.threeten.org/threeten-extra/apidocs/org/threeten/extra/YearQuarter.html, and more http://www.threeten.org/threeten-extra/apidocs/index.html.