tl;dr
要追踪历史时刻,请使用Instant https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/Instant.html作为类成员变量的类型。具体来说,这一时刻被视为日期和时间UTC https://en.wikipedia.org/wiki/Coordinated_Universal_Time.
public class UserObject() {
Instant createdAt ;
…
public void setCreatedAt( Instant instantArg ) {
this.createdAt = instantArg ;
{
}
用法,捕捉当前时刻。
UserObject user = new UserObject() ;
user.setCreatedAt( Instant.now() ) ;
用法,从数据库填充值。
UserObject user = new UserObject() ;
Instant instant = myResultSet.getObject( "when_created" , Instant.class ) ;
user.setCreatedAt( instant ) ;
JDBC 4.2 https://www.jcp.org/en/jsr/detail?id=221不需要支持Instant https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/Instant.html(UTC 时间)。如果您的驱动程序不支持该类,请切换到OffsetDateTime https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/OffsetDateTime.html这是必需的。
UserObject user = new UserObject() ;
OffsetDateTime odt = myResultSet.getObject( "when_created" , OffsetDateTime.class ) ;
user.setCreatedAt( odt.toInstant() ) ; // Convert from an `OffsetDateTime` (for any offset-from-UTC) to `Instant` (always in UTC).
呈现给用户,针对用户界面进行本地化。
user // Your business object.
.getCreatedAt() // Returns a `Instant` object.
.atZone( // Adjust from UTC to a time zone. Same moment, same point on the timeline, different wall-clock time.
ZoneId.of( "Pacific/Auckland" ) // Specify the user’s desired/expected time zone.
) // Returns a `ZonedDateTime` object.
.format( // Generate a `String` representing the value of date-time object.
DateTimeFormatter.ofLocalizedDateTime(
FormatStyle.FULL // Specify how long or abbreviated the string.
)
.withLocale( // Specify `Locale` to determine human language and cultural norms for localization.
Locale.CANADA_FRENCH
)
) // Returns a `String`.
请注意Locale https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Locale.html has nothing与时区有关,这是一个正交问题。上面的代码可能适用于来自以下地区的商务人士Québec https://en.wikipedia.org/wiki/Quebec谁正在旅行新西兰 https://en.wikipedia.org/wiki/New_Zealand。她想查看系统使用的挂钟时间kiwis https://en.wikipedia.org/wiki/Kiwi_(people)在她周围,但她更喜欢阅读用她的母语法语显示的文本。时区和区域设置都是最好只留给演示的问题;通常最好在代码的其余部分中使用 UTC。因此,我们定义了我们的成员变量createdAt
as an Instant
, with Instant
根据定义始终采用 UTC。
Avoid java.sql.Timestamp
- The
java.sql.Timestamp
, 随着java.sql.Date
, java.util.Date
, and Calendar
都是非常麻烦的旧日期时间类的一部分,这些类在几年前就被java.time类。
-
乔达时间 http://www.joda.org/joda-time/现在也被取代了java.time课程,如项目网站首页所述。
java.time
As of JDBC 4.2 https://docs.oracle.com/javase/8/docs/technotes/guides/jdbc/jdbc_42.html之后我们就可以直接兑换了java.time对象与数据库。
Instant
使用以下命令将当前时刻发送到数据库Instant
班级。这Instant http://docs.oracle.com/javase/10/docs/api/java/time/Instant.html类代表时间线上的一个时刻UTC https://en.wikipedia.org/wiki/Coordinated_Universal_Time分辨率为纳秒 https://en.wikipedia.org/wiki/Nanosecond(最多九 (9) 位小数)。
Instant instant = Instant.now() ; // Capture the current moment in UTC.
myPreparedStatement.setObject( … , instant ) ;
和检索。
Instant instant = myResultSet.getObject( … , Instant.class ) ;
ZonedDateTime
要通过特定地区(时区)的人们使用的挂钟时间的镜头来查看同一时刻,请应用ZoneId https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/ZoneId.html得到一个ZonedDateTime https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/ZonedDateTime.html.
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
LocalDateTime
is not片刻
.toLocalDateTime()。
绝不涉及LocalDateTime
代表特定时刻的类。该类故意缺少任何时区或与 UTC 的偏移量的概念。因此,它不能代表一个时刻,而是not时间轴上的一个点。这是一个模糊的想法潜在的大约 26-27 小时范围内的时刻(时区范围)。
About java.time
The java.time https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/package-summary.html框架内置于 Java 8 及更高版本中。这些课程取代了麻烦的旧课程legacy https://en.wikipedia.org/wiki/Legacy_system日期时间类,例如java.util.Date https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Date.html, Calendar https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Calendar.html, & SimpleDateFormat https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html.
要了解更多信息,请参阅甲骨文教程 http://docs.oracle.com/javase/tutorial/datetime/TOC.html。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310 https://jcp.org/en/jsr/detail?id=310.
The 乔达时间 http://www.joda.org/joda-time/项目,现在在维护模式 https://en.wikipedia.org/wiki/Maintenance_mode,建议迁移到java.time https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/package-summary.html类。
您可以交换java.time对象直接与您的数据库。用一个JDBC驱动程序 https://en.wikipedia.org/wiki/JDBC_driver符合JDBC 4.2 http://openjdk.java.net/jeps/170或稍后。不需要字符串,不需要java.sql.*
类。 Hibernate 5 和 JPA 2.2 支持java.time.
从哪里获取 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, Java SE 10 https://en.wikipedia.org/wiki/Java_version_history#Java_SE_10, Java SE 11 https://en.wikipedia.org/wiki/Java_version_history#Java_SE_11, and later - Part of the standard Java API with a bundled implementation.
-
Java 9 https://en.wikipedia.org/wiki/Java_version_history#Java_SE_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 (26+) 捆绑实现java.time类。
- For earlier Android (<26), a process known as API desugaring https://developer.android.com/studio/write/java8-support#library-desugaring brings a subset of the java.time https://developer.android.com/studio/write/java8-support-table functionality not originally built into Android.
- 如果脱糖不能提供您所需要的,三十ABP https://github.com/JakeWharton/ThreeTenABP项目适应三十后端口 http://www.threeten.org/threetenbp/(如上所述)到 Android。看如何使用 ThreeTenABP... http://stackoverflow.com/q/38922754/642706.