几年后我开始学习 Java 编程。我注意到 Java 8 中有一些新的日期/时间对象。
对于使用 Java 8 的持久数据 bean,我应该为日期/时间属性使用什么类型?是Instant
、ZoneDateTime
还是Date
?
最佳答案
我希望 bean 中的属性为 Instant
由于采用 UTC 格式,无论何时需要将其呈现给客户端,都可以将其转换为适当的 ZonedDateTime
.
Instant instant = Instant.now() ; // gives current instant of time in UTC.
//Can be converted to ZonedDateTime
ZoneId zone = ZoneId.of( "Asia/Kolkata" ) ) ;
ZonedDateTime zdt = instant.atZone( zone ) ;
关于java - Java 8 bean 日期属性使用什么类型 - Instant 或 ZoneDdateTime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44653435/