【问题标题】:Get `Instant` from `ZonedDateTime` in java.time从 java.time 中的`ZonedDateTime` 获取`Instant`
【发布时间】:2015-08-11 08:22:34
【问题描述】:

给定 Java 8 及更高版本的java.time package 中的ZonedDateTime,如何获得Instant

走另一条路(InstantZonedDateTime)很简单:

ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId ) ;

ZonedDateTimeInstant 需要转换为老式的java.util.Date 类。

java.util.Date date = Date.fromInstant( instant ) ;

【问题讨论】:

    标签: java datetime java-time


    【解决方案1】:

    你可以使用ChronoZonedDateTime接口中的toInstant() default方法(由ZonedDateTime继承。

    【讨论】:

    • 谢谢!我没有看到隐藏在“方法继承自…”块下的方法名称。
    • @BasilBourque 您的浏览器不支持ctrl + fcmd + f 吗? :P
    • @Tom 是的,我的网络浏览器确实支持 Command+F。找到 70 次出现的 Instance。我查看了主要方法列表中的每一个命中。两次。如果您查看InstantDatejava.sql.Timestamp 的文档页面,您会注意到几乎没有任何类型间转换方法是继承的,它们都在主方法列表中。
    • @BasilBourque 这个类叫Instant,而不是Instance:P。此外,一些 IDE 还支持良好的自动完成功能,可以编写类似 myZonedDateTime. 的内容,如果您随后编写 instant,它将找到方法 toInstant。 ...好吧,至少 IntelliJ 可以做到这一点:D.
    猜你喜欢
    • 2016-01-20
    • 2016-08-22
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 2019-04-16
    • 1970-01-01
    • 2015-10-14
    相关资源
    最近更新 更多