Link to home
Start Free TrialLog in
Avatar of boonleng
boonlengFlag for Malaysia

asked on

Incorrect days calculation in AIX

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;
      }
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

How's this?

  public static long fnGetDay(String fStrStartDate, String fStrEndDate) throws ParseException
  {
    SimpleDateFormat sdf = new SimpleDateFormat( "dd/MM/yyyy" ) ;

    Calendar lCalStartCalendar = Calendar.getInstance() ;
    Calendar lCalEndCalendar = Calendar.getInstance() ;

    lCalStartCalendar.setTime( sdf.parse( fStrStartDate ) ) ;
    lCalEndCalendar.setTime( sdf.parse( fStrEndDate ) ) ;

    int days = 0 ;

    while( lCalStartCalendar.compareTo( lCalEndCalendar ) <= 0 )
    {
      lCalStartCalendar.add( Calendar.DATE, 1 ) ;
      days++ ;
    }

    return days ;
  }
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of boonleng

ASKER

Thanks CEHJ, setting the Timezone solved the problem.

TimYates, your solution works as well. Just that I prefer not to use looping to count the days as the start date & end date may be 10+ years difference. However thanks for helping :)

Regards,
Boon leng
:-)