1

I just started to use Joda Time and for a week now I was satisfied with the results. I am trying to get the time range period between two dates of the format 'yyyy-MM-dd HH'.

Until I came to a test case I am writing and it seems to me the result is wrong:

2014-02-05 05 - 2016-02-04 05

Joda Results:

Years: 1 Months: 11 Days: 2 Hours: 0

Expected Results:

Years: 1 Months: 11 Days: 30 Hours: 0

I only need 1 more day to get it to a FULL 2 years....

I am not a QA but certainly this is a bug to me... I also talked to our QA about this and she agreed.

I want to raise a bug in Github but it pointed me to use Stackoverflow first.. any clarification will be greatly appreciated.

String hourFormat = "yyyy-MM-dd HH";
String startTime = "2014-02-05 05";
String endTime   = "2016-02-04 05";
DateTimeFormatter hourFormatter = DateTimeFormat.forPattern(hourFormat);
DateTime start = hourFormatter.parseDateTime(startTime);
DateTime end   = hourFormatter.parseDateTime(endTime  );
Interval interval = new Interval(start, end);
Period period = interval.toPeriod();
System.out.println(String.format(
  "Years: %d  Months: %d  Days: %d  Hours: %d",
  period.getYears(), period.getMonths(), period.getDays(), period.getHours()
));
YoYo
  • 9,157
  • 8
  • 57
  • 74
Rodel
  • 147
  • 1
  • 6
  • Please add a complete code sample – Krease Aug 23 '16 at 01:11
  • To solve my issue, I need to get the Exact Date after the 11th Month and calculate the Days Between that date and the End Date. **DateTime daysStartTime = endTime.plusHours(1);** **int days = Days.daysBetween(daysStartTime, end).getDays();** – Rodel Aug 23 '16 at 01:22
  • To clarify - your code sample that demonstrates he problem is incomplete. Please make it a self-contained compilable example. – Krease Aug 23 '16 at 01:24
  • @Krease Modified. – Rodel Aug 23 '16 at 01:25
  • @Krease My issue is the computation of the Period using JodaTime. My code comment is my own workaround. But still the issue exists and needs to be resolved. – Rodel Aug 23 '16 at 01:28
  • 4
    I found this ... http://stackoverflow.com/questions/20367759/incorrect-jodatime-period-years-months-days – Rodel Aug 23 '16 at 02:01
  • Main reason for difficulties: The Joda-Time-API uses implicit defaults. And in this case, the expectations what is the right default setting differ between API-designers and (most) API-users, i.e. should weeks be a part of default period calculation or not. I don't like implicit defaults and have therefore developed another alternative ([Time4J](http://time4j.net/javadoc-en/net/time4j/Duration.html#in-U...-)) where users must specifiy how they want the duration to be calculated (that is which units to be used). This is a little bit more writing effort but serves for clarity and readability. – Meno Hochschild Aug 23 '16 at 04:47

2 Answers2

2

Change the following lines of code

Interval interval =   new Interval(start, end);
Period period = interval.toPeriod();

To

Period period = new Period(start, end, PeriodType.yearMonthDay());
Rodel
  • 147
  • 1
  • 6
1

This does seem like a bug to me. Definitely, as Interval actually stores the begin and end dates internally, it should be able to make a proper conversion to a Period (interval.toPeriod()).

I bet when you simply calculate the Period directly from the two dates (without the intermediate step to an Interval), it will work just fine. Example:

Period period = new Period(start,end);

Or if the calculation is not correct with differences in week, you might need to use (as per Incorrect jodatime period - years months days):

Period period = new Period(start,end,PeriodType.yearMonthDay());

In the case of Java 8 java.time.* libraries (JSR-310), Interval was actually omitted, and only Period and Duration are allowed. So in Java 8, you could do something like this:

String hourFormat = "yyyy-MM-dd HH";
DateTimeFormatter hourFormatter = DateTimeFormatter.ofPattern(hourFormat);
LocalDateTime start = LocalDateTime.parse("2014-02-05 05",hourFormatter);
LocalDateTime end   = LocalDateTime.parse("2016-02-04 05",hourFormatter);

// Duration duration = Duration.between(start, end);
Period period = Period.between(start.toLocalDate(), end.toLocalDate());

System.out.println(
  String.format("Years: %d  Months: %d  Days: %d  Hours: xxx",
  period.getYears(), period.getMonths(), period.getDays())
);

Other obvious limitation is that Period in JSR-310 only has a resolution up-to days (not hours).

See also this for further reference: Is there a class in java.time comparable to the Joda-Time Interval?

Community
  • 1
  • 1
YoYo
  • 9,157
  • 8
  • 57
  • 74
  • yeah I do understand that it was not a bug :) since i just started using Joda Time due to my limitation of not using JDK 8. – Rodel Aug 25 '16 at 00:08