troubleshooting Question

Incorrect days calculation in AIX

Avatar of boonleng
boonlengFlag for Malaysia asked on
Java
4 Comments1 Solution623 ViewsLast Modified:
I have the following code to calculate number of days. It works fine in window platform, but in AIX the calculation is wrong. For example fnGetDay("01/04/2006", "30/06/2007") will return 456. But in AIX it will return 455.
This only happen to the month April, others months ok.
Any idea why or is there a better way to calculate number of days?
Thanks.

      public static long fnGetDay(String fStrStartDate, String fStrEndDate) {
            long lLngDayDelta;
            long lLngDayInMili;

            int lIntStartDay;
            int lIntStartMonth;
            int lIntStartYear;
            int lIntEndDay;
            int lIntEndMonth;
            int lIntEndYear;

            Calendar lCalStartCalendar;
            Calendar lCalEndCalendar;

            lLngDayDelta = 0;
            lLngDayInMili = 1000 * 60 * 60 * 24;

            // Extract start day values.
            lIntStartDay = Integer.parseInt(fStrStartDate.substring(0, 2));
            lIntStartMonth = Integer.parseInt(fStrStartDate.substring(3, 5));
            lIntStartYear = Integer.parseInt(fStrStartDate.substring(6, 10));

            // Extract end day values.
            lIntEndDay = Integer.parseInt(fStrEndDate.substring(0, 2));
            lIntEndMonth = Integer.parseInt(fStrEndDate.substring(3, 5));
            lIntEndYear = Integer.parseInt(fStrEndDate.substring(6, 10));

            // Create Calendar objects and set them to their correct values.
            lCalStartCalendar = Calendar.getInstance();
            lCalEndCalendar = Calendar.getInstance();

            lCalStartCalendar.set(lIntStartYear, lIntStartMonth - 1, lIntStartDay, 0,      0,0);
            lCalEndCalendar.set(lIntEndYear, lIntEndMonth - 1, lIntEndDay, 0, 0, 0);

            // Calculate the day delta.
            lLngDayDelta =
                  ((lCalEndCalendar.getTime().getTime()
                        - lCalStartCalendar.getTime().getTime())
                        / lLngDayInMili)
                        + 1;

            return lLngDayDelta;
      }
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros