Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Joda Time objects between the App layers

I'm considering using Joda-Time.
I'm wondering if I should pay attention of what type of object my Interfaces are returning.
Returning Joda-Time objects from my interface signature on the service layer means that every module that use it will have be dependent on Joda-Time instead of the common java.util.Date API.
Are you passing Joda objects around your App modules or do you write wrappers in specific part of your app?

like image 455
spike07 Avatar asked Mar 12 '10 13:03

spike07


People also ask

Does Joda DateTime have time zones?

An interval in Joda-Time represents an interval of time from one instant to another instant. Both instants are fully specified instants in the datetime continuum, complete with time zone.

What is Jodatime in Java?

Joda-Time provides a quality replacement for the Java date and time classes. 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). Joda-Time is licensed under the business-friendly Apache 2.0 licence.

What is Joda-Time format?

Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters. Mid level - pattern-based, like SimpleDateFormat. Low level - builder.

What is the use of Joda-Time?

Joda-Time provides support for multiple calendar systems and the full range of time-zones. The Chronology and DateTimeZone classes provide this support. Joda-Time defaults to using the ISO calendar system, which is the de facto civil calendar used by the world.


2 Answers

In the beginning, only return the most suitable type (Joda Objects in this case).

If you learn that someone has a problem with that (which probably won't happen too often), either add a converter method to the interface (so you have, say, getTime() and now getJavaTime() or getTimeInMillis()).

Or add a general purpose helper method which takes an Object (you can treat an unknown instance as Object anywhere in the code without having to import the actual Joda classes) and returns a plain Java object (java.util.Date).

like image 67
Aaron Digulla Avatar answered Sep 28 '22 07:09

Aaron Digulla


What is the alternative ? Transforming jodaTime objects into infamous Calendar/Date objects ?

You chose to get rid of these objects and that's a good decision. Now, if you let other layers use the java date API, they'll go into the kind of bugs and non-sensical behaviours you got rid of by using jodaTime.

I think you should do your users a favor and let them use jodaTime.

Of course it's a design decision that'll add in their code a dependency to jodaTime, but I don't find it questionnable, as you chose jodaTime to write less and better code, and so should they.

like image 20
Riduidel Avatar answered Sep 28 '22 05:09

Riduidel