I'm building an android app and I would do a sum of two periods, using jodatime library. Actually, I've a problem to manage the sign because I could have only one period with negative sign. I tried a code to do this, but the result is wrong for me.
example:
case 1:
time1 = 4:55
time2 = -7:10
time1 + time2 = -01:-55 --> this is wrong... I'm expected -3:45
case 2:
time1 = -4:55
time2 = 7:10
time1 + time2 = 4:05 --> this is correct
This is my sample code:
public static void main(String[] args)
{
String output;
String time1 = "-4:55";
String time2 = "7:10";
Duration durationSum = Duration.ZERO;
PeriodFormatter formatter = getFormatterBuilder();
Period period1 = formatter.parsePeriod(time1);
Duration duration1 = period1.toStandardDuration();
Period period2 = formatter.parsePeriod(time2);
Duration duration2 = period2.toStandardDuration();
output = formatter.print(durationSum.plus(duration1).plus(duration2).toPeriod());
System.out.println(output);
}
private static PeriodFormatter getFormatterBuilder()
{
return new PeriodFormatterBuilder()
.minimumPrintedDigits(2)
.printZeroAlways()
.appendHours()
.appendLiteral(":")
.appendMinutes()
.toFormatter();
}
What am I wrong?
Thanks!
Maybe the exotic sign handling of Joda-Time has confused you (like me on first try). In detail:
case 1:
time1 = 4:55
time2 = -7:10
time1 + time2 = -01:-55 --> this is wrong... I'm expected -3:45
The Joda-Time strategy for interpreting the negative sign means the sign to be applied on every single time component, NOT on the whole duration itself. So we don't have 55 minutes - 10 minutes but 55 minutes + 10 minutes:
time1 + time2 = (4:55) + (-7:10) = (+4 hours + 55 minutes) + (-7 hours + 10 minutes) = (4 * 60 minutes + 55 minutes) + (-7 * 60 minutes + 10 minutes) = 295 minutes - 410 minutes = -115 minutes = -1 hour - 55 minutes
We see that Joda-Time is consequently doing its sign strategy.
The same sign strategy confirms your second expectation however:
case 2:
time1 = -4:55
time2 = 7:10
time1 + time2 = 4:05 --> this is correct
time1 + time2 = (-4:55) + (7:10) = (-4 hours + 55 minutes) + (7 hours + 10 minutes) = (-4 * 60 minutes + 55 minutes) + (7 * 60 minutes + 10 minutes) = -185 minutes + 430 minutes = 245 minutes = 4 hours + 5 minutes
Important side note about the strategy of Joda-Time: As we can see in the examples given by you, Joda-Time violates the symmetry rule
-(time1 - time2) != (time2 - time1)
In math, this symmetry rule represents a specialized form of common distributive law where the minus sign can be interpreted as multiplication by factor -1. If Joda-Time had choosen the much more appropriate sign strategy to apply the sign on the whole duration instead then the calculation would yield:
case 1: (4:55) + (-7:10) = (4 hours + 55 minutes) + (-7 hours - 10 minutes) = 295 minutes - 430 minutes = -135 minutes = -2:15 (meaning -2 hours - 15 minutes)
case 2: (-4:55) + (7:10) = (-4 hours - 55 minutes) + (7 hours + 10 minutes) = -295 minutes + 430 minutes = 135 minutes = 2:15
This intuitive strategy would have ensured the mentioned symmetry rule above. But Jodas strategy (and now overtaken by Period
-class in JSR-310 aka java.time library in Java-8, too) is set in stone (unfortunately) and must be seen as "feature" if we like it or not.