# 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;
}
TimYates

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 )
{
days++ ;
}

return days ;
}
CEHJ

membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.