How to Use Joda Time in Eclipse?

Joda Time is another very useful library for your Android application development. It provides a quality library of classes to replace the Java JDK Date and Time classes.

This post is generally about how to import a library in eclipse for Android development, we just choose Joda Time library as a good example. Every time I have a post instructing to install a library, I’ll probably link into this post.

Ok, back to our main topic.

Why use Joda Time? Well, there are plenty of reasons, some include:

  • Easy to use – straightforward field accessors such as getDayOfWeek()
  • Up-to-date time zone calculations – based on public tz database
  • Maturity – active development since 2002, and
  • Open source (Apache).

It can save you a lot of time for tasks such as:

Joda Time: Step by Step

Step 1: Download the latest version of Joda Time.

How to Use Joda Time in Eclipse?
Download joda time in source forge.

Step 2: Extract the ZIP and find the JAR file.

Files included in the zip.
Files included in the zip.

Step 3,: Copy to JAR file to your project’s libs folder.

> Ctrl+C the Jar file
> find your libs folder (My libs folder is located in my c:/workspace/MyProject/libs/),
> and then Ctrl+V (paste) it there.

Refreshing (Click+F5) your project would look like this:


Step 4: Add the JAR file to your build path.

> Right click your project
> java build path
> libraries tab
> add jars button
> browse the joda time jar file in the libs folder
> order and export tab
> check the joda time jar file

If you want some visuals for this step 4, see this one:

Video Demo Permalink

Step 5: Test. You can have the following code on your sample project to see if it works.

We’ll have this code on our

package com.example.jodatimeexample;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.Days;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        String textViewContents = "Joda Time Results:nn";
        TextView tv = (TextView) findViewById(;
        int daysBetween = getNumDaysBetween("2013-09-01", "2013-09-02");
        tv.setText("Days between 2013-09-01 and 2013-09-02: " + daysBetween);

    public int getNumDaysBetween(String dateStart, String dateEnd){
        int numDays = 0;
            SimpleDateFormat formatYmd = new SimpleDateFormat("yyyy-MM-dd");
            Date d1 = formatYmd.parse(dateStart);
            Date d2 = formatYmd.parse(dateEnd);
            DateTime dt1 = new DateTime(d1);
            DateTime dt2 = new DateTime(d2);
            numDays = Days.daysBetween(dt1, dt2).getDays();
            // Days between 2013-09-01 and 2013-09-02: 1
        } catch (NullPointerException e) {
        } catch (Exception e) {
        return numDays;

activity_main.xml (layout file) used:

<RelativeLayout xmlns:android=""
    tools:context=".MainActivity" >

        android:text="Loading value..." />


The TextView will hold the output when you run the code above, and should look like this:

Days between 2013-09-01 and 2013-09-02: 1

Download Code

Just in case you want to download the code we used:

Further Readings

As for some pros and cons of using Joda Time, see Jon’s answer here.

Since Joda Time is another API to learn, you’ll need more examples codes, here are some few hundred Joda Time code examples.


Leave a Reply

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