3

I need help writing a function in Java that takes an input date and tells me the number of years, months, and days since the date.

For example, "July 1, 2005" would output "6 years, 2 months, 2 days"

  • 1
    Have you made any attempt? what are you struggling with? have you written it out in pseudocode? – luketorjussen Sep 03 '11 at 17:31
  • You'll see this type of feature already implemented by a number of great libraries, such as [joda-time](http://joda-time.sourceforge.net/). – Johan Sjöberg Sep 03 '11 at 17:31
  • Based on what? You said 6 years but what's the base date? – Tarik Sep 03 '11 at 17:31
  • @Braveyard - it says in the title, 'between Date and Now' – luketorjussen Sep 03 '11 at 17:33
  • 2
    @Richard - Welcome to Stack Overflow. This isn't a "Do my work for me" site. You will want to actually do some research, attempt to solve the problem yourself, and then ask here if you're stuck and need specific help. People will be more than willing to assist at that point. – Brian Roach Sep 03 '11 at 17:36
  • Oh that's simple `Calendar` operation... did you try deduction? – Nishant Sep 03 '11 at 17:38
  • This question doesn't make sense without a specific Locale, TimeZone and Calendar. – soc Sep 03 '11 at 17:42

2 Answers2

7

Use Joda Time - it makes it relatively easy:

import org.joda.time.*;

public class Test
{
    public static void main(String[] args)
    {
        LocalDate then = new LocalDate(2005, 7, 1);
        LocalDate today = new LocalDate();

        Period period = new Period(then, today, PeriodType.yearMonthDay());
        System.out.println(period); // P6Y2M2D
        System.out.println(period.getYears()); // 6
        System.out.println(period.getMonths()); // 2
        System.out.println(period.getDays()); //2
    }
}

(I vastly prefer the Joda API to Date/Calendar. It's much easier to use, partly due to generally preferring immutability.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Does Joda-Time supports Java version 1.4.2? – TheOneTeam Sep 03 '11 at 17:45
  • @Kit: You may need to find a 1.x version - 2.0 uses generics etc, so required Java 1.5 or higher I believe. – Jon Skeet Sep 03 '11 at 17:53
  • To make it even easier, look to the [`PeriodFormatterBuilder`](http://www.joda.org/joda-time/apidocs/org/joda/time/format/PeriodFormatterBuilder.html) class. For example, a formatter that prints years and months, like "15 years and 8 months", can be constructed as follows: `PeriodFormatter yearsAndMonths = new PeriodFormatterBuilder() .printZeroAlways() .appendYears() .appendSuffix(" year", " years") .appendSeparator(" and ") .printZeroRarelyLast() .appendMonths() .appendSuffix(" month", " months") .toFormatter();` – Basil Bourque May 27 '15 at 04:21
0

java.time

Quoted below is a notice from the home page of Joda-Time:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

You can use java.time.Period which is modelled on ISO-8601 standards and was introduced with Java-8 as part of JSR-310 implementation.

Solution using java.time, the modern Date-Time API:

import java.time.LocalDate;
import java.time.Period;

public class Main {
    public static void main(String[] args) {
        LocalDate startDate = LocalDate.of(2005, 7, 1);
        LocalDate endDate = LocalDate.of(2011, 9, 3);
        Period period = startDate.until(endDate);
        System.out.println(period);

        // Custom format
        String formatted = String.format("%d years, %d months, %d days", period.getYears(), period.getMonths(),
                period.getDays());
        System.out.println(formatted);
    }
}

Output:

P6Y2M2D
6 years, 2 months, 2 days

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110