Search code examples
javaandroiddatetimejodatimeperiod

How calculate negative period using JodaTime


I'm building an Android app and I need to calculate a sum of a period (hours and minutes). Pratically, I build a method that make this, but I've a problem with negative period.

In example:

First case --> is correct

Input 1: 3:00
Input 2: 0:30

Output: 3:30 // This is correct --> 3:00 + 0:30 = 3:30

Second case --> is wrong

Input 1: 3:00
Input 2: -0:30

Output: 3:30 // This is wrong...

Output Expected: 2:30 // --> 3:00 + (-0:30) = 2:30

This is my code:

public String OperationsHH_Sum(String time1, String time2)
    {
        PeriodFormatterBuilder builder = new PeriodFormatterBuilder();
        builder.minimumPrintedDigits(2);
        builder.printZeroAlways();
        builder.appendHours();
        builder.appendLiteral(":");
        builder.appendMinutes();
        PeriodFormatter pf = builder.toFormatter();

        Period period1 = pf.parsePeriod(time1);
        Period period2 = pf.parsePeriod(time2);
        Period normalized;
        Period total;

        total = period1.plus(period2);
        normalized = total.normalizedStandard(PeriodType.time());

        return pf.print(normalized);
    }

What am I doing wrong?

Thanks!!

EDIT:

I solved my problem...this is the code:

public String OperationsHH_Sum(String time1, String time2)
    {

        String[] time1_out, time2_out;

        if (time1.contains("-"))
        {
            time1_out = time1.split(":");
            time1_out[1] = "-" + time1_out[1];
            time1 = time1_out[0] + ":" + time1_out[1];
        }

        if (time2.contains("-"))
        {
            time2_out = time1.split(":");
            time2_out[1] = "-" + time2_out[1];
            time2 = time2_out[0] + ":" + time2_out[1];
        }

        PeriodFormatterBuilder builder = new PeriodFormatterBuilder();
        builder.minimumPrintedDigits(2);
        builder.printZeroAlways();
        builder.appendHours();
        builder.appendLiteral(":");
        builder.appendMinutes();
        PeriodFormatter pf = builder.toFormatter();

        Period period1 = pf.parsePeriod(time1);
        Period period2 = pf.parsePeriod(time2);
        Period zero = pf.parsePeriod("0:00");
        Period normalized;
        Period total;

        total = period1.plus(period2);
        boolean negative = false;

        if (total.toStandardDuration().isShorterThan(zero.toStandardDuration()))
        {
            negative = true;
        }
        normalized = total.normalizedStandard(PeriodType.time());

        return (negative ? "-" : "") + pf.print(normalized);
    }

Solution

  • The Problem lies within the parsing of the String. In YodaTime the representation of negative values is -HH:-MM so your -0:30 means "minus zero hours, plus 30 minutes" which is the same as 00:30.

    To solve your problem you can do one of two things:

    1. Write negative numbers in the expected format, meaning "-HH:-MM", e.g. "-0:-30"
    2. Remove the negative sign before parsing and decide yourself if it should be added or removed.