× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Hi all,
I came across very strange problem when roll method in GregorianCalendar.
When rolling down from given date - calendar skips 04/04/2004 and rolls to the 
04/03/2004
When I roll up - dates are correct.
Here is the code that I use, please test...

    try {
      SimpleDateFormat mmddyyyy = new SimpleDateFormat("MM/dd/yyyy");
      SimpleDateFormat wd = new SimpleDateFormat("E");

      String[] weekdates = new String[7];
      String[] weekdays = new String[7];

      GregorianCalendar cal = new GregorianCalendar();      
      java.util.Date date = mmddyyyy.parse("04/09/2004");

      cal.setTime(date);

      for (int i=0; i<weekdates.length; i++) {
        cal.roll(Calendar.DAY_OF_YEAR, false);
        date = cal.getTime();
        weekdays[i] = wd.format(date);
        weekdates[i] = mmddyyyy.format(date);
      }
      // print result
      System.out.println("Result of rolling down");
      for (int i=0; i<weekdates.length; i++) {
        System.out.println(weekdays[i]+"; "+weekdates[i]);
      }

      // start rollup
      date = mmddyyyy.parse("04/02/2004");

      cal.setTime(date);

      for (int i=0; i<weekdates.length; i++) {
        cal.roll(Calendar.DAY_OF_YEAR, true);
        date = cal.getTime();
        weekdays[i] = wd.format(date);
        weekdates[i] = mmddyyyy.format(date);
      }
      // print result
      System.out.println("Result of rolling up");
      for (int i=0; i<weekdates.length; i++) {
        System.out.println(weekdays[i]+"; "+weekdates[i]);
      }

    } catch (Exception e) {
      e.printStackTrace();
    }

// my print output: please not that there is no Sunday when roll down used!!!!
Result of rolling down 
Thu; 04/08/2004 
Wed; 04/07/2004 
Tue; 04/06/2004 
Mon; 04/05/2004 
Sat; 04/03/2004 
Fri; 04/02/2004 
Thu; 04/01/2004 

Result of rolling up 
Sat; 04/03/2004 
Sun; 04/04/2004 
Mon; 04/05/2004 
Tue; 04/06/2004 
Wed; 04/07/2004 
Thu; 04/08/2004 
Fri; 04/09/2004 


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.