tl;dr
OffsetDateTime.parse( "2010-12-27T10:50:44.000-08:00" )
ISO 8601
输入字符串格式定义在ISO 8601 https://stackoverflow.com/q/4542679/642706标准,一系列日期时间格式。
避免旧的日期时间类
问题和其他答案使用与最早版本的 Java 捆绑在一起的旧的过时日期时间类。避开他们。现在被 java.time 类取代。
使用java.time
您的输入字符串以与 UTC 的偏移量 https://en.wikipedia.org/wiki/UTC_offset。所以我们解析为OffsetDateTime http://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html object.
java.time 类在解析/生成字符串时默认使用 ISO 8601 格式。因此无需指定格式模式。
OffsetDateTime odt = OffsetDateTime.parse( "2010-12-27T10:50:44.000-08:00" );
如果您想将此日期时间值查看为 UTC 时间轴上的某个时刻,请提取Instant
.
Instant instant = odt.toInstant();
时区是一个偏移量plus一组用于处理夏令时 (DST) 等异常情况的规则。如果您考虑了时区,请应用ZoneId
得到一个ZonedDateTime
目的。时间轴上的同一时刻,但通过不同的方式查看挂钟时间 https://en.wikipedia.org/wiki/Wall-clock_time.
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = odt.atZoneSameInstant( z ); // Same moment on the timeline, but viewed through a different wall-clock time.
About 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对象直接与您的数据库。用一个JDBC驱动程序 https://en.wikipedia.org/wiki/JDBC_driver符合JDBC 4.2 http://openjdk.java.net/jeps/170或稍后。不需要字符串,不需要java.sql.*
类。
从哪里获取 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.