tl;博士
改用 java.time 类。
Instant.now() // Capture current moment in UTC. Always in UTC, by definition.
……和……
ZonedDateTime.now() // Capture current moment as seen through the wall-clock time of the people in the region of the time zone used by default in this JVM.
详情
正如其他人所说,您误解了这些课程中涉及的概念。 java.util.Date 代表 UTC 中的时刻,始终采用 UTC†,从不在其他时区。所以问题中看到的代码是无意义的。你太辛苦了!
java.time
Joda-Time 项目现在位于maintenance mode,团队建议迁移到java.time 类。 Joda-Time 和 java.time 之间的许多概念相似,因为这两个项目都是由同一个人 Stephen Colebourne 领导的。
当您准备好迁移时,使用 Instant 代替 java.util.Date。
Instant instant = Instant.now() ; // Capture the current moment in UTC.
instant.toString(): 2018-01-23T12:34:56.123456789Z
使用ZonedDateTime 代替java.util.Calendar 来表示通过特定区域(时区)的挂钟时间看到的时刻。
以continent/region 的格式指定proper time zone name,例如America/Montreal、Africa/Casablanca 或Pacific/Auckland。切勿使用 3-4 个字母的缩写,例如 EST 或 IST,因为它们不是真正的时区,没有标准化,甚至不是唯一的 (!)。
ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = instant.atZone( z ) ; // Same moment, same point on the timeline, different wall-clock time.
如果您只想要分区时间,则可以跳过Instant 作为快捷方式。
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ; // Capture the current moment as seen by people in a certain time zone.
您可以通过提取Instant 对象从那里到达UTC。
Instant instant = zdt.toInstant() ; // Same moment, same point on the timeline, but viewed with the wall-clock time of UTC.
† 实际上,在java.util.Date 深处分配了一个时区,但这与我们在这里的讨论无关。令人困惑?是的。 许多原因之一是避免旧的Date/Calendar 和相关的旧日期时间类造成的混乱。
关于java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.Date、Calendar 和 SimpleDateFormat。
Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。
要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310。
您可以直接与您的数据库交换 java.time 对象。使用符合JDBC 4.2 或更高版本的JDBC driver。不需要字符串,不需要java.sql.* 类。
从哪里获得 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如Interval、YearWeek、YearQuarter 和more。