tl;博士
要跟踪历史时刻,请使用Instant 作为类成员变量的类型。具体来说,这一刻在UTC 中被视为日期和时间。
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 不需要支持Instant(UTC 时间)。如果您的驱动程序不支持该类,请切换到必需的OffsetDateTime。
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 与时区无关,这是一个正交问题。上面的代码可能适用于来自Québec 的商务人士,他在New Zealand 中旅行。她想查看她周围的kiwis 使用的挂钟时间,但她更喜欢用她的母语法语阅读它的文字显示。时区和语言环境都是最好留给演示的问题;通常最好在其余代码中使用 UTC。因此,我们将成员变量createdAt 定义为Instant,而Instant 根据定义始终为UTC。
避免java.sql.Timestamp
-
java.sql.Timestamp、java.sql.Date、java.util.Date 和 Calendar 都是多年前被 java.time取代的非常麻烦的旧日期时间类的一部分> 课程。
-
Joda-Time 现在也被 java.time 类所取代,如项目网站首页所述。
java.time
从JDBC 4.2以后,我们可以直接和数据库交换java.time对象了。
Instant
使用Instant 类将当前时刻发送到数据库。 Instant 类代表UTC 时间线上的时刻,分辨率为nanoseconds(最多九 (9) 位小数)。
Instant instant = Instant.now() ; // Capture the current moment in UTC.
myPreparedStatement.setObject( … , instant ) ;
和检索。
Instant instant = myResultSet.getObject( … , Instant.class ) ;
ZonedDateTime
要通过特定地区(时区)的人们使用的挂钟时间的镜头查看同一时刻,请应用ZoneId 以获取ZonedDateTime。
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
LocalDateTime不是片刻
.toLocalDateTime().
在表示特定时刻时,切勿涉及 LocalDateTime 类。该类故意缺少任何时区或与 UTC 偏移的概念。因此,它不能代表一个时刻,不是时间线上的一个点。对于大约 26-27 小时(时区范围)范围内的潜在时刻,这是一个模糊的概念。
关于java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.Date、Calendar 和 SimpleDateFormat。
要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310。
Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。
您可以直接与您的数据库交换 java.time 对象。使用符合JDBC 4.2 或更高版本的JDBC driver。不需要字符串,不需要java.sql.* 类。 Hibernate 5 & JPA 2.2 支持 java.time。
从哪里获取 java.time 类?