This tutorial shows how we can use joda date time API.

Java’s standard date and time classes (mainly java.util.Date and java.util.Calendar) have limited functionality and have a number of design problems. The fact that many of constructors and methods of java.util.Date are deprecated.

Joda Time has a better thought out API than what is available in Java’s date and time standard library. There are classes for timestamps with or without a timezone, classes for holding only a date (year, month, day) or only a time of day, classes for periods, durations and intervals, it supports the ISO 8601 format (which is the standard format in XML documents) and much more.

Now JDK 8 is out and it has a new date and time API which is similar to Joda Time. For more information please read package java.time.
Prerequisites

JDK 1.6 or later
Eclipse 3.2 or later
Maven 2 or later
Joda Date Time API

Now we will see how we can make this happen step by step

Step 1. Create a standalone maven project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.

maven-arctype-quickstart

Now enter the required fields (Group Id, Artifact Id) as shown below

Group Id : com.roytuts
Artifact Id : joda-date-time

Step 2. Add joda-time dependency to the pom.xml file.

<!-- joda date time -->
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.7</version>
</dependency>

Step 3. Write a class which will show how to use joda date time

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class JodaDateTimeTest {

  /**
   * @param args
   */
  public static void main(String[] args) {
    DateTime dateTime = new DateTime(DateTimeZone.forOffsetMillis(0));
    System.out.println(dateTime);

    LocalDateTime date = LocalDateTime.now();
    DateTimeZone tz = DateTimeZone.getDefault();
    DateTime dateTime2 = date.toDateTime(tz);
    System.out.println(date);
    System.out.println(dateTime2);

    DateTime dateTimeCustom = new DateTime("2015-05-15T12:58:50");
    System.out.println(dateTimeCustom);
    System.out.println(new Date(dateTimeCustom.getMillis()));

    DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd hh:mm:ss");
    String dt = dateTime.toString(fmt);
    System.out.println(dt);
  }

}

Step 4. Now run the above class and see the output in the console.

2015-06-10T07:03:47.576Z
2015-06-10T12:33:47.702
2015-06-10T12:33:47.702+05:30
2015-05-15T12:58:50.000+05:30
2015-05-15
2015-06-10 07:03:47

That’s all. Thank you for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit | Email Me

Leave a Reply

Your email address will not be published. Required fields are marked *