将Joda时间迁移到Java 8
Joda:
UserObject user = new UserObject()
user.setCreatedAt(new DateTime(rs.getTimestamp("columnName")));`迁移到Java 8
这是我的代码;它可以编译;我怀疑它是否能工作:
ZonedDateTime.ofInstant(rs.getTimestamp("columnName").toLocalDateTime().toInstant(ZoneOffset.UTC),ZoneId.of("UTC")));在某些情况下,日期是错误的。有什么建议吗?
发布于 2018-07-18 19:35:07
这似乎是可行的:
ZonedDateTime.ofInstant(rs.getTimestamp("columnname").toInstant(), ZoneId.of("UTC"))发布于 2018-07-17 05:28:17
tl;dr
要跟踪历史中的某个时刻,请使用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的支持(以协调世界时为单位)。如果您的驱动程序不支持该类,请切换到所需的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与时区无关,这是一个正交问题。上面的代码可能是为在New Zealand旅行的Québec商务人员编写的。她想看到周围的kiwis使用的挂钟时间,但她更喜欢用她的母语法语阅读它的文本显示。时区和区域设置都是最好留给演示文稿的问题;通常情况下,最好在代码的其余部分使用UTC。因此,我们将成员变量createdAt定义为Instant,根据定义,Instant始终在UTC中。
避免java.sql.Timestamp
正如该项目网站的首页所述,
java.sql.Timestamp以及java.sql.Date、java.util.Date和Calendar都是非常麻烦的旧date-time类的一部分,这些date-time类在几年前也被java.time date-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
框架内置于Java8和更高版本中。这些类取代了麻烦的旧legacy日期时间类,如java.util.Date、Calendar和SimpleDateFormat。
要了解更多信息,请参阅。和搜索堆栈溢出,以获得许多示例和解释。规范为JSR 310。
现在在maintenance mode中的项目建议迁移到java.time类。
您可以直接与数据库交换java.time对象。使用与JDBC 4.2或更高版本兼容的JDBC driver。不需要字符串,也不需要java.sql.*类。Hibernate 5和JPA2.2支持java.time。
从哪里获取java.time类?
中的
发布于 2018-07-17 03:07:53
使用以下内容:
rs.getTimestamp("columnName").toInstant()
.atZone(ZoneId.[appropriate-zone-ID-here])您需要使用适合该区域的ZoneId (您可以尝试使用ZoneId.systemDefault()作为开始)。
有关各种Java-Time API之间差异的更多详细信息,请参阅this great answer。
https://stackoverflow.com/questions/51368395
复制相似问题