Migrate from Joda to Java(JSR-310)

Migrate from Joda to Java(JSR-310)

So, I recently migrated Joda to Java(JSR-310) in a 12-years-old project.

In this article, I will describe what I did, and provide some tips along the way. I will also share some of the lessons I learned from this migration.


Why?

That is simple, because we want to make our life easier.
After migration, the project will benefit from

  • Native support from the ecosystem, for example JPA
  • Build faster with less dependency
  • bug fix/security patch can be received immediately

In addition, quoting from Joda Time

Joda-Time is the de facto standard date and time library for Java prior to Java SE 8. Users are now asked to migrate to java.time (JSR-310).

So if you are working in legacy project using Joda, it is time to consider migrating to JSR-310.

How?

To begin, like any kind of migration, make sure you have enough test coverage before making any change.
If no, it’s time to leave and add more test.

Congratulate if you continue to read, I will just share my approach below:

  1. Do some research, good starting point https://blog.joda.org/2014/11/converting-from-joda-time-to-javatime.html
  2. Write utilities for converting between Java<->Joda class.
  3. Convert class by class, I recommend the order from easy to hard LocalDate->LocalTime->LocalDateTime->Instant->DateTime->Period/Duration.
    This may as simple as just using Regex to replace the import statement and the constructor to java of , but may be difficult in some case.
    I created a repository to summarise common class/API conversion and tricky stuff.
  4. For exceptional case that you need more time to handle.
    Use the utilities in (2) to help you, e.g. change
    void someComplexDateTimeFunction(DateTime   dateTime) {  
    // some api difficult to convert in a few mins 
    } 
    
    to
     void someComplexDateTimeFunction(ZonedDateTime zonedDateTime) {  
          DateTime dateTime =   DateTimeUtils.toDateTime(zonedDateTime) // some api difficult to convert in a few mins 
      } 
    
    The main idea is to make the project compile first, and left difficult part later.
  5. Fix any failed test, and then go back to those case left behind in (4).
    Feel free to drop a comment if you got stuck.
  6. Time to delete the Joda dependency.

Notes

  1. Joda-Time only supports time up to millis where Java supports nano.
  2. Check any changes required by other libraries. Some libraries, such as Jackson, Hibernate, and Thymeleaf, may require changes when you migrate to JSR-310.
  3. Date Time formatting/parsing is one of the most tricky part, do consult documentation as same symbol may have different meaning in Joda and Java.

Lesson Learn

  • The knowledge of Java Date Time API.
    For example, the different between toXXX vs toXXXPart in java.time.Duration
  • Why JSR 310 isn’t a copy of Joda
    I have this question long time ago and understand the reason behind after doing this migration
  • Sometimes errors are hidden in the dark, thanks to Sinbio’s comment
    , which save me from a production issue. That is also why I think write this article maybe useful.

I hope this article has been helpful. If you have any questions, please feel free to leave a comment below.

Comments