0

So I have 2 date pickers for a start and end time, which give back a DateTime.

I need to calculate the duration between these two times in the format of "hours hr mins hr", so for example, "1 hr 30 mins". If there is a day difference, it will display "24 hr", so we use hours instead of days.

The problem I am having is that whatever way I got this working, it breaks if the user selects a month ahead. They never should choose a month ahead but are given the option to.

I get this error:

java.lang.UnsupportedOperationException: Unable to normalize as PeriodType is missing either years or months but period has a month/year amount: P1M3W6DT1H
        at org.joda.time.Period.normalizedStandard(Period.java:1640)

And my code is:

Period durationHoursAndMinutes =  new Period(startDate.toLocalDateTime(), endDate.toLocalDateTime()).normalizedStandard(PeriodType.time());
PeriodFormatter formatter = new PeriodFormatterBuilder()
  .appendHours()
  .appendSuffix(getResources().getString(R.string.mytrips_tripview_rail_waitTime_hr))
  .appendSeparator(" ")
  .appendMinutes()
  .appendSuffix(getResources().getString(R.string.mytrips_tripview_rail_waitTime_min))
.toFormatter();

if(durationHoursAndMinutes.getMinutes() > 0){
  return formatter.print(durationHoursAndMinutes);
}else {
  return null;
}

I looked at the code for the PeriodType and it says to throw the exception if the months != 0, which it won't be. Just wondering if there is a way I can solve this. Thanks for any help

Ayohaych
  • 5,099
  • 7
  • 29
  • 51
  • Can you give a short but complete example demonstrating the problem? (You probably don't need your formatter - and it almost certainly doesn't need to be on Android. A tiny console app demonstrating the problem would make this a lot clearer.) – Jon Skeet Mar 25 '15 at 12:03
  • Note that your claim that months won't be != 0 is clearly wrong given the reported failing period: P1M3W6DT1H – Jon Skeet Mar 25 '15 at 12:04
  • @JonSkeet I just saw in the code it said something about throwing the exception if months != 0 – Ayohaych Mar 25 '15 at 12:11
  • Indeed. And in your case, months == 1 - that's what the "1M" in "P1M3W6DT1H" means. That period is 1 month, 3 weeks, 6 days and 1 hour. – Jon Skeet Mar 25 '15 at 12:12
  • @JonSkeet so that's why its crashing right? that's what i meant above by it wont be 0 – Ayohaych Mar 25 '15 at 12:16
  • Oh, that was a confusing way of putting it then. I thought you meant "the condition wouldn't be satisfied, i.e. months really *will* be 0". Anyway, I've given a solution - it would still be helpful if you'd show a short but complete program demonstrating the problem. – Jon Skeet Mar 25 '15 at 12:17
  • @JonSkeet Yeah sorry my bad. Yep your solution worked great. I will try do up a short program in a while, in work at the moment so don't really have time right now – Ayohaych Mar 25 '15 at 12:32

1 Answers1

6

I suspect the simplest way to do this is just to pass the PeriodType into the constructor instead:

new Period(
    startDate.toLocalDateTime(),
    endDate.toLocalDateTime(),
    PeriodType.time());

Then you don't need to perform any other normalization.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Great that worked! Thanks for the solution, I used the normalization because I saw it on another stack overflow solution. – Ayohaych Mar 25 '15 at 12:31