https://bz.apache.org/bugzilla/show_bug.cgi?id=60029

            Bug ID: 60029
           Summary: Problem with Days360 method for the month of february
           Product: POI
           Version: 3.14-FINAL
          Hardware: PC
            Status: NEW
          Severity: normal
          Priority: P2
         Component: POI Overall
          Assignee: [email protected]
          Reporter: [email protected]

Hello,

There is a problem with the Days360 class in POI
(org.apache.poi.ss.formula.functions.Days360.class).

The result of the evaluation is not the same as the Excel method when the start
date is the last day of february and the end date is the last day of the month:
Start date: 02/28/2018
End date: 03/31/2018
Result in Excel: 30
Result in Apache POI: 31

The problem is in the method getEndingDate: you should not be using realStart
but the result of getStartingDate.

Here is a solution:

    private static double evaluate(double d0, double d1, boolean method) {
        Calendar realStart = getDate(d0);
        Calendar realEnd = getDate(d1);
        int startingDate[] = getStartingDate(realStart, method);
        int endingDate[] = getEndingDate(realEnd, startingDate, method);
        return
            (endingDate[0]*360+endingDate[1]*30+endingDate[2])-
            (startingDate[0]*360+startingDate[1]*30+startingDate[2]);
    }

    private static int[] getEndingDate(Calendar realEnd, int startingDate[],
boolean method) {
        Calendar d = realEnd;
        int yyyy = d.get(Calendar.YEAR);
        int mm = d.get(Calendar.MONTH);
        int dd = Math.min(30, d.get(Calendar.DAY_OF_MONTH));

        if (method == false && realEnd.get(Calendar.DAY_OF_MONTH) == 31) {
            if (startingDate[2] < 30) {
                d.set(Calendar.DAY_OF_MONTH, 1);
                d.add(Calendar.MONTH, 1);
                yyyy = d.get(Calendar.YEAR);
                mm = d.get(Calendar.MONTH);
                dd = 1;
            } else {
                dd = 30;
            }
        }

        return new int[]{yyyy,mm,dd};
    }

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to