• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Joda time

 
Ranch Hand
Posts: 348
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

can anybody give me reason why people are encouraged to leave JDK dates APIs in favor of jodatime (prior Java 8)? is it faster? thread safe? what's so special about this lib?
Thanks
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,

I think this question can be answered with just a little bit of help by googling joda time vs java time

tldr: The Joda time API was easier to use and avoid bugs with than the java one.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are unfortunately lots of things wrong with the old java.util.Date and java.util.Calendar classes. To name a few things:

Class Date has the wrong name. Despite the name, it does not represent a date (indicating a specific day of a specific month in a specific year). Instead, it is really a timestamp (internally it stores a number of milliseconds since 01-01-1970, 00:00:00 GMT). It is not very well suited for storing just a date (day, month, year), or just a time of day (hours, minutes, seconds, milliseconds), but people try to use it that way anyway.

There is a lot of misunderstanding about the classes Date and Calendar. People ask questions like "I want to have a Date object in the format yyyy-MM-dd" or "I want to convert my Date object from one timezone to another". Date objects do not have a format and do not know anything about timezones, so you cannot have a Date object with a certain format or in a certain timezone. I've seen a lot of cargo cult code, sometimes written by experienced Java developers, trying to do strange things with Date objects because the programmer misunderstood what a Date object is exactly.

Classes Date and Calendar make no distinction between "absolute" points in time and date and time values that do not indicate an "absolute" point in time. Joda Time specifically does make this distinction. Anything that is an Instant in Joda Time represents an "absolute" moment in time. A class such as LocalDateTime is not an "absolute" moment in time - for example, what 01-05-2015 09:22:00 means exactly depends on the timezone you're in. This distinction is often important in software, and Java's old date and time API makes it hard.

Classes Date and Calendar are not immutable and not thread-safe. They should have been designed as value objects and should have been immutable. Also, SimpleDateFormat is not thread-safe - it's not safe to make a public static final SimpleDateFormat variable in a multi-threaded program; if multiple threads try to use it at the same time, you'll get strange results.

Months are numbered starting at 0 in class Calendar, making it very easy to make mistakes: cal.set(2015, 5, 1); sets the calendar to 1 June 2015 instead of 1 May 2015 because 0 = January, 1 = February, ..., 4 = May, 5 = June, ...

Class Calendar has a method named getTime() that returns a Date object. An absolutely unintuitive method name.

The old Java date and time API has (almost) no support for working with durations and periods, or alternative chronologies (for example, the Buddhist or Arabic calendar systems).

Joda Time has a much better designed API for working with date and time. It's too bad it took so long before Java got a decent date and time API.
 
Marshal
Posts: 79964
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can read about the new date and time classes in the Java® Tutorials. Unfortunately they only work in Java8. They are supposed to be similar to Joda time.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The new date and time API in Java 8 has been designed by the author of Joda Time (Stephen Colebourne), so it's not a coincidence that the new API is similar to Joda Time. But it's not the same, there are some notable differences.

If you're still on Java 7 and you want to use the new Java 8 date and time API, then there's a backport available - a JAR file that you can use with Java 7 - that provides the same classes as the Java 8 API. You can find it on http://www.threeten.org/
 
Campbell Ritchie
Marshal
Posts: 79964
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote: . . . then there's a backport available - . . .

Now that is very useful
 
Do not threaten THIS beaver! Not even with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic