将 Joda time Instant 转换为 Java time Instant

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38532361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:30:04  来源:igfitidea点击:

converting Joda time Instant to Java time Instant

javatimejodatimejava-timejava.time.instant

提问by user123475

I have an instance of Instant (org.joda.time.Instant) which I get in some api response. I have another instance from (java.time.Instant) which I get from some other call. Now, I want to compare these two object to check which one get the latest one. How would it be possible?

我有一个 Instant (org.joda.time.Instant) 的实例,我在一些 api 响应中得到了它。我有另一个来自 (java.time.Instant) 的实例,它是从其他调用中获得的。现在,我想比较这两个对象以检查哪一个是最新的。怎么可能?

回答by discipliuned

getMillis()from joda.time can be compared to toEpochMilli()from java.time.

getMillis()from joda.time 可以与toEpochMilli()from java.time进行比较。

Class documentation:

类文档:

Example code.

示例代码。

java.time.Instant myJavaInstant = 
    java.time.Instant.ofEpochMilli( myJodaInstant.getMillis() ) ;

Going the other way.

走另一条路。

// Caution: Loss of data if the java.time.Instant has microsecond
// or nanosecond fraction of second.
org.joda.time.Instant myJodaInstant = 
    new org.joda.time.Instant( myJavaInstant.toEpochMilli() ); 

回答by xYan

You can convert from joda Instant to java's (the datetime and formatting are just an example):

您可以从 joda Instant 转换为 java(日期时间和格式只是一个例子):

org.joda.time.Instant.parse("10.02.2017 13:45:32", DateTimeFormat.forPattern("dd.MM.yyyy HH:mm:ss")).toDate().toInstant()

So you call toDate()and toInstant()on your joda Instant.

所以,你打电话toDate()toInstant()你的乔达瞬间。