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!