Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting Joda time Instant to Java time Instant

Tags:

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?

like image 738
user123475 Avatar asked Jul 22 '16 17:07

user123475


People also ask

What is the replacement of Joda-time?

Correct Option: D. In java 8,we are asked to migrate to java. time (JSR-310) which is a core part of the JDK which replaces joda library project.

Is Joda-time format followed in Java 8?

Joda-Time is an API created by joda.org which offers better classes and having efficient methods to handle date and time than classes from java. util package like Calendar, Gregorian Calendar, Date, etc. This API is included in Java 8.0 with the java.

Is Joda-time deprecated?

So the short answer to your question is: YES (deprecated).

What is Java instant time?

In Java language, the Instant Class is used to represent the specific time instant on the current timeline. The Instant Class extends the Object Class and implements the Comparable interface.


2 Answers

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

Class documentation:

  • org.joda.time.Instant::getMillis
  • java.time.Instant::toEpochMilli

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() );  
like image 151
discipliuned Avatar answered Sep 17 '22 18:09

discipliuned


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.

like image 32
xYan Avatar answered Sep 17 '22 18:09

xYan