converting Joda time Instant to Java time Instant

18,851

Solution 1

getMillis() from joda.time can be compared to 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() ); 

Solution 2

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

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.

Share:
18,851

Related videos on Youtube

user123475
Author by

user123475

Updated on June 04, 2022

Comments

  • user123475
    user123475 about 2 years

    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?

    • discipliuned
      discipliuned almost 8 years
      for comparison you could get the millis from each
  • DusanV
    DusanV over 5 years
    the call to parse makes this answer confusing, but actually calling toDate returns a java.util.Date, which can be used to get the Instant...
  • xYan
    xYan over 5 years
    hence the brackets: (the datetime and formatting are just an example)