javaandroidjodatimeandroid-jodatime

How to calculate No of Years, Months and days between two dates using Joda-Time


I'm using below code to calculate No of Years, Months and days between two dates using Joda-Time

public void getDateDiff(View view) {

    DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy").withLocale(Locale.US);
    DateTime startDate = null;
    DateTime endDate = null;

    // expected output 23...2....29

    try {
        startDate = formatter.parseDateTime("01/09/1995");
        endDate = formatter.parseDateTime("30/11/2018");

        Period period = new Period(startDate, endDate);

        Log.e("No Of Years : ", period.getYears() + " Years, ");
        Log.e("No Of Months : ", period.getMonths() + " Months, ");
        Log.e("No Of Days : ", period.getDays() + " Days, ");

        tvResult.setText(String.format("No Of Years : %d Years, \nNo Of Months : %d Months, \nNo Of Days : %d Days, ", period.getYears(), period.getMonths(), period.getDays()));

    } catch (Exception e) {
        e.printStackTrace();
    }


}

The expected output of above code is( checked using this site and using one app in google play store)

23 years, 2 months, 29 days

but I'm getting below result using same date

/goku.com.demo E/No Of Years :: 23 Years, 
/goku.com.demo E/No Of Months :: 2 Months, 
/goku.com.demo E/No Of Days :: 1 Days, 

Can any one help me to find issue what I'm missing in above code

I'm using using below joda-time Dependency.

implementation 'joda-time:joda-time:2.9.9'

P.S. already checked below links

If need more information please do let me know. Thanks in advance. Your efforts will be appreciated.


Solution

  • The reason that your Joda Time code does not work, is because if you don't provide a PeriodType object, then the standard PeriodType is used. The standard period type defines not only years, months and days to be included in the Period calculation, but also weeks. And you're not displaying the number of weeks, which is 4 in your case. If you write period.getWeeks() it'll return 4.

    In order to make it work, you have to provide a PeriodType as third argument to the Period constructor. If you change the declaration of period to

    Period period = new Period(startDate, endDate, PeriodType.yearMonthDay());
    

    then it'll work. The period calculation will then only use the year, month and day fields.

    But

    ...it is better to migrate to the new Java Date and Time API available in the java.time package, if you are using Java 8 or above. Don't get me wrong, Joda Time is a very good date and time API. Java 8's date and time API is heavily influenced on Joda Time, because of its quality. Its lead developer is actually the same person, Stephen Colebourne.

    But Joda Time is now in maintenance mode, and on its website users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

    Using Java 8 Date and Time API

    DateTimeFormatter f = DateTimeFormatter.ofPattern("dd/MM/yyyy").withLocale(Locale.US);
    
    // expected output 23...2....29
    LocalDate startDate = LocalDate.parse("01/09/1995", f);
    LocalDate endDate = LocalDate.parse("30/11/2018", f);
    Period period = Period.between(startDate, endDate);
    

    Backport to Java 6 and 7; Android support

    Most of the java.time functionality is back-ported to Java 6 and Java 7 in the ThreeTen-Backport project, also led by Stephen Colebourne. Further adapted for earlier Android API levels (below 26) in the ThreeTenABP project. See How to use ThreeTenABP.

    Note that there are some differences between Joda Time and the Java Date and Time API. In particular, the Period class from the Java Date and Time API is less comprehensive. However, the Period class from the Java Date and Time API provides the functionality which meets your requirement.


    I see that this code using java.time is actually the same as @MaxXFrenzY's. This is not because of copy-paste, that is because the new Java Date and Time API is designed to be both unambiguous and straightforward.