Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Joda Time

Joda Time

A time manipulation library for Java SDK's Calendar replacement

Avatar for Nikolay Georgiev

Nikolay Georgiev

May 20, 2013
Tweet

More Decks by Nikolay Georgiev

Other Decks in Programming

Transcript

  1. 3

  2. 4

  3. 5

  4. 9 Content  Joda-Time - introduction  Joda-Time concepts 

    JDK Calendar. Why we should use Joda-Time?  Specifics and advantages  Integration  Demo
  5. 10 Joda-Time Intorduction {0}  Joda-Time provides a Java library

    for date and time handling  It is an open-source software under ASF2* license  It totally replaces the JDK Date and Calendar classes * http://www.apache.org/licenses/LICENSE-2.0)
  6. 11 Joda-Time Introduction {1}  Joda is 100 percent interoperable

    with the JDK, so you don't need to replace all of your Java code, only the parts that do date/time calculations.  Joda-Time makes time and date values easy to manage, manipulate, and understand. Ease of use, in fact, is Joda's primary design goal
  7. 15 Chronology  Examples of chronologies supported by Joda are:

    ISO8601 (default) Gregorian/Julian Gregorian Julian Buddhist (Thai) Coptic Ethiopic Islamic  And by JDK Calendar: Buddhist Japanese Gregorian
  8. 22 Immutability  Like the java.lang.String. Most (but not all)

    of the Joda objects are immutable.  So when changing an object new object is returned (it is often forgotten)!  Thread-safety
  9. 23 Constructors  Date - a JDK instant  Calendar

    - a JDK calendar  String - in ISO8601 format  Long - in milliseconds  any Joda-Time datetime class This list is extensible. A little type-safety for extensibility. Takes Object!
  10. 27 Why should we use Joda-Time instead of JDK Calendar

    and Date?  January 1, 2000 (00:00h).  No java.util.Date - java.util.Calendar should be used.
  11. 28 Let's use java.util.Calendar then... // Remember the date from

    the previous slide? // 2000.01.01, 00:00 (a.k.a “The Millennium bug”) Calendar calendar = Calendar.getInstance(); calendar.set(2000, Calendar.JANUARY, 1, 0, 0, 0); Using Joda-Time it would look pretty much like this: DateTime dateTime = new DateTime(2000, 1, 1, 0, 0, 0, 0);
  12. 29 Adding 90 days to an instant and printing the

    result, the JDK way Calendar calendar = Calendar.getInstance(); calendar.set(2000, Calendar.JANUARY, 1, 0, 0, 0); SimpleDateFormat sdf = new SimpleDateFormat("E MM/dd/yyyy HH:mm:ss.SSS"); calendar.add(Calendar.DAY_OF_MONTH, 90); System.out.println(sdf.format(calendar.getTime()));
  13. 30 Adding 90 days to an instant and printing the

    result, using Joda DateTime dateTime = new DateTime(2000, 1, 1, 0, 0, 0, 0); System.out.println( dateTime.plusDays(90) .toString("E MM/dd/yyyy HH:mm:ss.SSS");
  14. 32 Joda to the rescue DateTime dateTime = new DateTime(2000,1,1,0,0,0,0);

    System.out.println( dateTime.plusDays(45) .plusMonths(1) .dayOfWeek() .withMaximumValue() .toString("E MM/dd/yyyy HH:mm:ss.SSS"));
  15. 33 Formatting and Parsing  Slow in the JDK's SimpleDateFormat

     40ms per format/parse  Not thread-safe  Using DateTimeFormatter – 4ms (10 times faster)  Joda-Time .toString() uses DateTimeFormatter Speed Test Demo
  16. 52 Wake up, we'll finish in a moment!  Joda-Time

    – very effective tool for manipulating date and time  Date calculation, formatting, parsing and printing  Replaces JDK Calendar and Date
  17. 53