6

I'm using JDateChooser and I'm making a program that output the list of dates between the selected dates. for example:

date1= Jan 1, 2013  // Starting Date

date2= Jan 16,2013  // End Date

then it will output

Jan 2, 2013...
Jan 3, 2013.. 
Jan 4, 2013..

and so on... until it reaches the end date.

I already finish working on the my program that once you click a date on the JDatechooser it will output the end date automatically. (selected date + 15 days = end dates)

I download the JCalendar or JDateChooser here: http://www.toedter.com/en/jcalendar/

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Maguzu
  • 433
  • 3
  • 7
  • 14

1 Answers1

31

You should try using Calendar, which will allow you to walk from one date to another...

Date fromDate = ...;
Date toDate = ...;

System.out.println("From " + fromDate);
System.out.println("To " + toDate);

Calendar cal = Calendar.getInstance();
cal.setTime(fromDate);
while (cal.getTime().before(toDate)) {
    cal.add(Calendar.DATE, 1);
    System.out.println(cal.getTime());
}

Updated

This example will include the toDate. You can rectify this by creating a second calendar that acts as the lastDate and subtracting a day from it...

Calendar lastDate = Calendar.getInstance();
lastDate.setTime(toDate);
lastDate.add(Calendar.DATE, -1);

Calendar cal = Calendar.getInstance();
cal.setTime(fromDate);
while (cal.before(lastDate)) {...}

This will give you all the dates "between" the start and end dates, exclusively.

Adding Dates to an ArrayList

List<Date> dates = new ArrayList<Date>(25);
Calendar cal = Calendar.getInstance();
cal.setTime(fromDate);
while (cal.getTime().before(toDate)) {
    cal.add(Calendar.DATE, 1);
    dates.add(cal.getTime());
}

2018 java.time Update

Time moves on, things improve. Java 8 introduces the new java.time API which has superseded the "date" classes and should, as a preference, be used instead

LocalDate fromDate = LocalDate.now();
LocalDate toDate = LocalDate.now();

List<LocalDate> dates = new ArrayList<LocalDate>(25);

LocalDate current = fromDate;
//current = current.plusDays(1); // If you don't want to include the start date
//toDate = toDate.plusDays(1); // If you want to include the end date
while (current.isBefore(toDate)) {
    dates.add(current));
    current = current.plusDays(1);
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • That makes a nice change ;) – MadProgrammer May 28 '13 at 07:18
  • 1
    *cough* Joda Time *cough* – Moritz Petersen May 28 '13 at 07:57
  • @MoritzPetersen Agreed, I thought it might be a little over kill for this, as we're actually calculating differences, just listing the dates. – MadProgrammer May 28 '13 at 07:59
  • can i change the date format like this MM-dd-yyyy? – Maguzu May 28 '13 at 08:13
  • `Date` doesn't care. You can use a [`SimpleDateFormat`](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) to provide custom date formats if you need, but `Date` and `Calendar` won't care. – MadProgrammer May 28 '13 at 08:16
  • @MadProgrammeris there a way to add the dates to arraylist while looping? – Maguzu Jun 03 '13 at 07:51
  • @JohnPaulMagat Yes. In the while loop, instead of just printing the result to stdout, you can add the result to an `ArrayList` – MadProgrammer Jun 03 '13 at 08:13
  • @MadProgrammer i tried this `while (cal2.getTime().before(newDateString)) { cal2.add(Calendar.DATE, 1); arraylist.add(cal2.getTime()); }` but there is an error on the `add` why? or i'm doing it wrong? – Maguzu Jun 03 '13 at 08:39
  • @JohnPaulMagat I added an example. I can only assume that the `ArrayList` either did allow for `Date` objects or wasn't initalised – MadProgrammer Jun 03 '13 at 08:58
  • @MadProgrammer Still same error here is my implementation: `ArrayList arraylist = new ArrayList(); //get the days between two dates then print it List dates = new ArrayList(25); Calendar cal2 = Calendar.getInstance(); cal2.setTime(toDate); while (cal2.getTime().before(newDateString)) { cal2.add(Calendar.DATE, 1); dates.add(cal2.getTime()); System.out.println(dates); }` – Maguzu Jun 04 '13 at 00:57
  • @MadProgrammer i try doing it with an iterator .. `String datelist=(format.format(cal2.getTime())); List strlist2 = new ArrayList(); strlist2.add(datelist); Iterator itr22 = strlist2.iterator(); while (itr22.hasNext() ){ List strlist3 = new ArrayList(); // strlist3.add(itr22.next()); System.out.println(strlist3); }` but i dont know what is next to this. – Maguzu Jun 04 '13 at 00:58
  • @JohnPaulMagat `String` is not a `Date`. `cal1.getTime()` will return a `Date`. You `ArrayList` can't contain any other object type other then `String` – MadProgrammer Jun 04 '13 at 00:58
  • @JohnPaulMagat Your second code example doesn't make sense. You're creating a new `ArrayList` called `strlist3`, adding a value to and then discarding it... – MadProgrammer Jun 04 '13 at 01:00
  • @MadProgrammer i can't understand i just followed your example, the second code is i'm trying to add the dates using iterator is that possible? i hope your not mad.. :D – Maguzu Jun 04 '13 at 01:17
  • @JohnPaulMagat Not mad ;) - My example creates a list of dates ...`ArrayList`, you're trying to use `String`, this is why you can't add values to your list – MadProgrammer Jun 04 '13 at 01:19
  • @JohnPaulMagat Okay, try something like...`String datelist = (format.format(cal2.getTime())); List strlist2 = new ArrayList(); strlist2.add(datelist); Iterator itr22 = strlist2.iterator(); List strlist3 = new ArrayList(); while (itr22.hasNext()) { String next = itr22.next(); strlist3.add(next); System.out.println(next); } System.out.println(strlist3);` – MadProgrammer Jun 04 '13 at 01:25
  • i got it! already working.. `ArrayList dates = new ArrayList(25); Calendar cal2 = Calendar.getInstance(); cal2.setTime(toDate); while (cal2.getTime().before(newDateString)) { cal2.add(Calendar.DATE, 1); String datelist=(format.format(cal2.getTime())); dates.add(datelist); } System.out.println(dates); System.out.println(dates.get(3));` now i can call the dates individually... thanks dude! :D – Maguzu Jun 04 '13 at 01:32