Question

I am trying to find the difference between two dates and display it in the format "0 years 0 months 0 days 0 hours 0 minutes 0 seconds". I have created a method that takes two DateTime objects and return a string representing the difference.

Here is the method:

public static String getReadableDifference(DateTime start, DateTime end){

    int years = Years.yearsBetween(start, end).getYears();
    end = end.minusYears(years);
    int months = Months.monthsBetween(start, end).getMonths();
    end = end.minusMonths(months);
    int days = Days.daysBetween(start, end).getDays();
    end = end.minusDays(days);
    int hours = Hours.hoursBetween(start, end).getHours();
    end = end.minusHours(hours);
    int minutes = Minutes.minutesBetween(start, end).getMinutes();
    end = end.minusMinutes(minutes);
    int seconds = Seconds.secondsBetween(start, end).getSeconds();

    StringBuilder returnString = new StringBuilder(); 

    if(years != 0){
        returnString.append(years);
        returnString.append(" years ");
    }
    if(months != 0){
        returnString.append(months);
        returnString.append(" months ");
    }
    if(days != 0){
        returnString.append(days);
        returnString.append(" days ");
    }
    if(hours != 0){
        returnString.append(hours);
        returnString.append(" hours ");
    }
    if(minutes != 0){
        returnString.append(minutes);
        returnString.append(" minutes ");
    }
    if(seconds != 0){
        returnString.append(seconds);
        returnString.append(" seconds ");
    }

    return returnString.toString();
}

I then created two DateTime objects and calculated the difference using the method. If I initialize the DateTime objects as so, (startin this case equals 2014-03-28T12:56:38.137+01:00 and end would be read from a file in the full program.)

 DateTime start = new DateTime();
 DateTime end = DateTime.parse("2015-03-28T12:56:35.137+01:00");

the resulting output would be 11 months 31 days -3 seconds, but if I initialized the DateTime objects like so,

DateTime now = DateTime.parse(new DateTime().toString());
DateTime end = DateTime.parse("2015-03-28T12:56:35.137+01:00");

the resulting output would be 11 months 30 days 23 hours 59 minutes 57 seconds as would have been expected in the original initialization.

My Question is: Why does the first method of initialization produces the incorrect output, why did the second method produce the correct output, and why are the outputs not the same?

While it does not explain the issue, this is the method of getting the time difference that I decided to use:

 String string = new Period(start, end, PeriodType.yearMonthDayTime())
    .toString(new PeriodFormatterBuilder()
    .appendYears().appendSuffix(" year", " years").appendSeparator(" ")
    .appendMonths().appendSuffix(" month", " months").appendSeparator(" ")
    .appendDays().appendSuffix(" day", " days").appendSeparator(" ")
    .appendHours().appendSuffix(" hour", " hours").appendSeparator(" ")
    .appendMinutes().appendSuffix(" minute", " minutes").appendSeparator(" ")
    .appendSeconds().appendSuffix(" second", " seconds").toFormatter());     
Was it helpful?

Solution

Your problem must be somewhere else in your code, possibly you are modifying the start/now dates.

The following gives equal results:

    DateTime start = new DateTime();
    DateTime now = DateTime.parse(start.toString());
    DateTime end = DateTime.parse("2015-03-28T12:56:35.137+01:00");

    System.out.println("Start=" + start);
    System.out.println("Now  =" + now);

You can also use the Period from JodaTime to do the pretty printing, perhaps a bit easier to read:

    Period period = new Period(start, end, PeriodType.yearMonthDayTime());
    System.out.print(period.getYears() + " years, ");
    System.out.print(period.getMonths() + " months, ");
    // etc.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top