tl;博士
现代方法使用 java.time 类和 ISO 8601 字符串。
阅读。
Instant // Represent a moment in UTC with a resolution of nanoseconds.
.ofEpochMilli(
Long.getLong( incomingText )
) // Returns a `Instant` object.
.atZone( // Adjust from UTC to some time zone. Same moment, same point on the timeline, different wall-clock time.
ZoneId.of( "Europe/Paris" )
) // Returns a `ZonedDateTime` object.
写作。
ZonedDateTime
.of(
LocalDate.of( 2018 , Month.JANUARY , 23 ) ,
LocalTime.of( 15 , 35 ) ,
ZoneId.of( "Europe/Paris" )
) // Returns a `ZonedDateTime` object.
.toInstant() // Returns an `Instant`. Adjust from a time zone to UTC. Same moment, same point on the timeline, different wall-clock time.
.toEpochMilli() // Returns a `long` integer number primitive. Any microseconds or nanoseconds are ignored, of course.
如果您的警报管理器尚未现代化以处理 java.time 对象,请使用添加到旧类的新方法在旧类和现代类之间进行转换。
java.util.Date d = java.util.Date.from( instant ) ;
……和……
Instant instant = d.toInstant() ;
java.time
麻烦的旧日期时间类被 java.time 类所取代。
在 UTC 时间,分辨率为纳秒,使用 Instant。
Instant instant = Instant.now() ; // Capture the current moment in UTC.
您只需要毫秒来满足您的需求,因此请截断任何微秒和纳秒。
Instant instant = Instant.now().truncatedTo( ChronoUnit.MILLIS ) ;
要按日期和时间确定时刻,需要时区。时区对于确定日期至关重要。对于任何给定的时刻,日期在全球范围内因区域而异。例如,Paris France 中午夜后几分钟是新的一天,而 Montréal Québec 中仍然是“昨天”。
如果没有指定时区,JVM 会隐式应用其当前的默认时区。该默认值可能在运行时(!)期间change at any moment,因此您的结果可能会有所不同。最好将您的 desired/expected time zone 明确指定为参数。
以continent/region 的格式指定proper time zone name,例如America/Montreal、Africa/Casablanca 或Pacific/Auckland。切勿使用 3-4 个字母的缩写,例如 EST 或 IST,因为它们不是真正的时区,没有标准化,甚至不是唯一的 (!)。
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ;
如果你想使用 JVM 当前的默认时区,请求它并作为参数传递。如果省略,则隐式应用 JVM 的当前默认值。最好是明确的,因为默认值可能会在任何时候在运行时被 JVM 中任何应用程序的任何线程中的任何代码更改。
ZoneId z = ZoneId.systemDefault() ; // Get JVM’s current default time zone.
或者指定一个日期。您可以通过数字设置月份,1 月至 12 月的编号为 1-12。
LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ; // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.
或者,更好的是,使用预定义的Month 枚举对象,一年中的每个月一个。提示:在整个代码库中使用这些 Month 对象,而不是仅仅使用整数,以使您的代码更具自记录性、确保有效值并提供 type-safety。
LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;
结合时间,LocalTime。
LocalTime lt = LocalTime.of( 14 , 0 ) ;
将它们全部包装成一个ZonedDateTime 对象。
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;
通过提取 Instant 来调整到 UTC。
Instant instant = zdt.toInstant() ;
提取自 UTC 1970 年第一刻的纪元参考以来所需的毫秒数。同样,请注意,在提取毫秒时,Instant 中的任何微/纳米都将被忽略。
long milliseconds = instant.toEpochMilli() ; // Be aware of potential data loss, ignoring any microseconds or nanoseconds.
使用 Long 类从存储中读取这些毫秒作为文本。
long milliseconds = Long.getLong( incomingText ) ;
Instant instant = Instant.ofEpochMilli( milliseconds ) ;
要通过特定地区(时区)的人们使用的挂钟时间的镜头看到那一刻,请申请ZoneId 以获取ZonedDateTime。
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
要生成表示该值的文本,请使用DateTimeFormatter.ofLocalizedDateTime 自动本地化。
提示:考虑将您的日期时间值以标准ISO 8601 格式而不是毫秒计数写入存储。人类无法有意义地读取毫秒数,这使得调试和监控变得棘手。
String output = instant.toString() ;
2018-10-05T20:28:48.584Z
Instant instant = Instant.parse( 2018-10-05T20:28:48.584Z ) ;
关于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。