21
import java.text.SimpleDateFormat;
import java.util.Date;
import org.joda.time.*;

public class Test {

  public static void main(String[] args) {

String dateStart = "01/01/2000 05:30";
String dateStop = "02/2/2001 06:31";

SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm");

Date d1 = null;
Date d2 = null;

try {
    d1 = format.parse(dateStart);
    d2 = format.parse(dateStop);

    DateTime dt1 = new DateTime(d1);
    DateTime dt2 = new DateTime(d2);

    System.out.print(Years.yearsBetween(dt1, dt2).getYears() + " years, ");
    System.out.print(Months.monthsBetween(dt1, dt2).getMonths() % 52 + " months, ");
    System.out.print(Weeks.weeksBetween(dt1, dt2).getWeeks() % 4 + " weeks, ");
    System.out.print(Days.daysBetween(dt1, dt2).getDays() % 7 + " days, ");
    System.out.print(Hours.hoursBetween(dt1, dt2).getHours() % 24 + " hours, ");
    System.out.print(Minutes.minutesBetween(dt1, dt2).getMinutes() % 60 + " minutes, ");


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

}

}

I want to output the number of years, months, weeks, days, hours, and minutes between two dates using Joda-Time. My question is where do I implement the number of weeks in a month (which in never constant). I don't think my %'s are right either.

When run I get:

1 years, 13 months, 0 weeks, 6 days, 1 hours, 1 minutes, 
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
MrAwesome8
  • 269
  • 1
  • 2
  • 9
  • If you just use a [`Period`](http://joda-time.sourceforge.net/apidocs/org/joda/time/Period.html), will that give you what you're after? – Rob Hruska Feb 10 '14 at 02:11
  • What do you mean by a Period? I know there are durations, intervals and periods but I do not know the difference. – MrAwesome8 Feb 10 '14 at 02:12
  • http://stackoverflow.com/questions/2653567/joda-time-whats-the-difference-between-period-interval-and-duration – Rob Hruska Feb 10 '14 at 02:15
  • What do you mean "number of days"? You need to define how you want to treat the 23-hour and 25-hour days that result when daylight savings begins and ends. – Dawood ibn Kareem Feb 10 '14 at 02:15
  • Once minutes exceeds 60 then 1 is added to hours. once hours exceeds 24 then 1 is added to days. once days exceeds 24 then one is added to weeks. etc. – MrAwesome8 Feb 10 '14 at 02:19
  • @DavidWallace Note that `Period` does account for that: *"...periods represent an abstracted definition of a time period (eg. a day may not actually be 24 hours, it might be 23 or 25 at daylight savings boundary)..."* – Rob Hruska Feb 10 '14 at 02:20
  • Yes, @RobHruska, you are right about that. However, judging from OP's previous comment, that's not actually what he/she wants. Either that, or he/she hasn't thought about it too much. – Dawood ibn Kareem Feb 10 '14 at 02:22
  • @DavidWallace True. Periods are *probably* what the OP wants - if not, a `Duration` might be appropriate. If neither, then I have no idea, and pity the person implementing their own date subtraction logic. :) – Rob Hruska Feb 10 '14 at 02:24
  • Yes. It's always worth stopping and thinking about these things, just to be sure that you are getting what you think you're getting, and to appreciate all the reasons _why_ you shouldn't be trying to roll your own date arithmetic library. – Dawood ibn Kareem Feb 10 '14 at 02:30
  • Rob was correct. What I needed was a Period. – MrAwesome8 Feb 10 '14 at 02:37
  • 1
    FYI, Joda-Time provides a trio of classes defining a span of time: [Interva](http://www.joda.org/joda-time/apidocs/org/joda/time/Interval.html), [Period](http://www.joda.org/joda-time/apidocs/org/joda/time/Period.html), and [Duration](http://www.joda.org/joda-time/apidocs/org/joda/time/Duration.html). Each has a different angle, and they interoperate with each other. I suggest becoming familiar with all three. – Basil Bourque Feb 10 '14 at 07:42

1 Answers1

66

Period gives you this out of the box.

Period period = new Period(d1, d2);
System.out.print(period.getYears() + " years, ");
System.out.print(period.getMonths() + " months, ");
// ...

To prettify and get a little more control over the output, you can use a PeriodFormatterBuilder.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192