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:
- Get number of days between two dates
- Get the amount of days of a given month
- Set DateTime to start of month
- Find out first day of a particular week
Joda Time: Step by Step
Step 1: Download the latest version of Joda Time.

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

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:
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 MainActivity.java
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.app.Activity; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String textViewContents = "Joda Time Results:nn"; TextView tv = (TextView) findViewById(R.id.tvDisplay); tv.setText(textViewContents); 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; try{ 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) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return numDays; } }
activity_main.xml (layout file) used:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/tvDisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Loading value..." /> </RelativeLayout>
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: JodaTimeExample.zip
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.