-1

I'm making a program that will generate the dates between two dates. The dates are from a DateChooser. When I click a button the dates between the two dates is generated like this :

    date1 Jun 5, 2013  
    date2 June 20, 2013

Using this code

    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(toDate);
    while (cal2.getTime().before(newDateString)) {
        cal2.add(Calendar.DATE, 1);
        String datelist=(format.format(cal2.getTime()));
        System.out.println(datelist);

It will generate this output

2013-06-18
2013-06-19
2013-06-20
2013-06-21
2013-06-22
2013-06-23
2013-06-24
2013-06-25
2013-06-26
2013-06-27
2013-06-28
2013-06-29
2013-06-30
2013-07-01
2013-07-02

My problem here is that I want to output this on a JTable. I tried calling each string from the datelist like this System.out.println(arrayImageList.get(2)); but it didn't work. How can I output datelist to JTable or call every element from datelist individually?

    final String oldy= ("yyyy-MM-dd");
    final String newy = ("MMMM dd, yyyy");

    SimpleDateFormat formatty = new SimpleDateFormat(oldy);
    java.util.Date datey=null;

    //format the final date output to MMMM-dd-yyyy
    try {
        datey=formatty.parse(datelist);
        java.util.Date newqwe2 = new SimpleDateFormat(oldy).parse(datelist);
        String eqweqwe = new SimpleDateFormat(newy).format(newqwe2);

        ArrayList<String> arrayImageList = new ArrayList<String>();
        List<String> strlist2 = new ArrayList<String>();

        arrayImageList.addAll((List<String>) Arrays.asList(eqweqwe));                                                                   

        for(int i = 0; i < arrayImageList.size(); i++){
            strlist2.add(arrayImageList.get(i));

            System.out.println(strlist2.get(2));

Sorry for the long post but I just want you guys to get whole process in case there is something missing.

Craig
  • 1,390
  • 7
  • 12
Maguzu
  • 433
  • 3
  • 7
  • 14

1 Answers1

0

Seems to me you are doing a lot of unnecessary work. If you work with Calendar objects you can do that very efficient. Put both dates in objects of Calendar and name them toDate and fromDate. You just run a while loop that keeps increasing the fromDate until it is larger than toDate and print it out.

Calendar fromDate;
Calendar toDate;

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

while(fromDate.compareTo(toDate) < 0)
{
   System.out.println(df.format(fromDate.getTime()));
   fromDate.add(Calendar.DATE, 1);
}

You can also use the loop in order to other things (e.g. populate a List or Model for a JTable). In that case just make a list of String or Date objects and simply place the return from getTime() or format() in the list/model.

Daniel Lerps
  • 5,256
  • 3
  • 23
  • 33
  • "Put both dates in objects of Calendar and name them toDate and fromDate. " What do you mean by this? – Maguzu Jun 03 '13 at 04:31