How do I get number of days between two dates in Joda?

The following code snippet show you how to get days between two dates. You can use the Days.daysBetween() method and pass the dates you want to calculate. This method return a Days object. To get the number in days you call the getDays() method.

package org.kodejava.example.joda;

import org.joda.time.DateMidnight;
import org.joda.time.Days;

public class DaysBetweenDemo {
    public static void main(String[] args) {
        // Define the start and end dates. We use the DateMidnight
        // class to make sure that the calculation start from the
        // midnight.
        DateMidnight start = new DateMidnight("2010-02-01");
        DateMidnight end = new DateMidnight("2012-02-07");

        // Get days between the start date and end date.
        int days = Days.daysBetween(start, end).getDays();

        System.out.println("Days between " +
                start.toString("yyyy-MM-dd") + " and " +
                end.toString("yyyy-MM-dd") + " = " +
                days + " day(s)");
    }
}

Here is the result of the program:

Days between 2010-02-01 and 2012-02-07 = 736 day(s)
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada

Leave a Reply