Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

migrate from Joda time library to Java time (Java 8)

Our software architect made decision to remove Joda time library from our project dependencies and to use features of Java 8 time. I am doing research of our project and I am trying to find all places where we are using Joda time lib and determine what actually we are using (joda LocalDate, DateTime, etc.).
I am not an expert of Joda time library and I not an expert of Java 8 time, so I have some questions:
In code I saw a lot of places like this:

DateTime jodaDateTime = new DateTime();
doSomething(jodaDateTime.toDate()); 

I am a little bit confused.

(Q1) Does it make any sense to use joda DateTime in this particular case? I believe, that we can use just classic java.util.Date and we do not need Joda:

java.util.Date date = new Date();
doSomething(date);

Correct?

Also, I saw this piece of code:

org.joda.time.DateTime jodaDateTime = new DateTime();
org.joda.time.DateTime jodaDateTimeInPast = jodaDateTime.minusSeconds(60);
doSomething(jodaDateTimeInPast.toDate());

I think, that java 8 time API provides the great support for calculating any dates in the past, so I have idea how to replace the piece of code above to Java 8:

LocalDatetime java8DateTime = LocalDateTime.now();
LocaldateTime java8DateTimeInPast = java8DateTime.minusSeconds(60);
Date java8Date = Date.from(java8DateTimeInPast.atZone(ZoneId.systemDefault()).toInstant());
doSomething(java8Date);

(Q2) Have I done it in the right way? I am not sure on 100%. I just found this link with a table: "Converting from Joda-Time to java.time" There are I found info, that I need to use java.time.ZonedDateTime or java.time.OffsetDateTime classes, but not java.time.LocalDateTime, because LocalDateTime -

Same concept - date and time without time-zone

Please confirm that my refactoring is correct (for Q1 and Q2).

like image 994
user471011 Avatar asked Jan 27 '16 17:01

user471011


2 Answers

Most questions about conversion should be handled by my blog on the topic.

Q1a: There is no point in creating a Joda-Time object only to change back to java.util.Date. However, the method should probbaly take Instant or ZonedDateTime:

Instant instant = Instant.now();
doSomething(instant);  // method changed to take Instant

Note that Instant is the closest match to java.util.Date.

Q1b: This case involves a time-zone, so should use ZonedDateTime.

ZonedDateTime java8DateTime = ZonedDateTime.now();  // uses ZoneId.systemDefault()
ZonedDateTime java8DateTimeInPast = java8DateTime.minusSeconds(60);
doSomething(java8DateTimeInPast.toInstant());

Again, this assumes that doSomething is changed to take an Instant.

The key to using java.time properly is to think about what data type is correct for each piece of data.

  • Just a date? Then use LocalDate
  • Just a time? Then use LocalTime
  • Date, time and time-zone? Then use ZonedDateTime
  • Just a timestamp with no other info? Then use Instant
  • A network date-time format with offset (not zone)? Then use OffsetDateTime.
like image 107
JodaStephen Avatar answered Nov 05 '22 18:11

JodaStephen


You are doing it right for both cases. The only comment I might add that java 8 time supersedes java.util.Date so you might want to think of replacing your methods doSomething(java.util.Date) with doSomething(java.time.temporal.TemporalAccessor). However, java.util.Date has not been declared depricated just yet, so you don't have to. In my experience, java 8 time package is very flexible and comprehensive package. I found some funny bugs with Joda even though Joda was a good package for its time.

like image 38
Michael Gantman Avatar answered Nov 05 '22 18:11

Michael Gantman