Avatar of boonleng
Flag 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?

      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 =
                        - lCalStartCalendar.getTime().getTime())
                        / lLngDayInMili)
                        + 1;

            return lLngDayDelta;

Avatar of undefined
Last Comment

8/22/2022 - Mon

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 ;

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

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 :)

Boon leng

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes