how to get a list of dates between two dates in java

后端 未结 22 1539
余生分开走
余生分开走 2020-11-22 13:24

I want a list of dates between start date and end date.

The result should be a list of all dates including the start and end date.

22条回答
  •  逝去的感伤
    2020-11-22 13:53

    List dates = new ArrayList();
    String str_date = "DD/MM/YYYY";
    String end_date = "DD/MM/YYYY";
    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    Date startDate = (Date)formatter.parse(str_date); 
    Date endDate = (Date)formatter.parse(end_date);
    long interval = 1000 * 60 * 60; // 1 hour in milliseconds
    long endTime = endDate.getTime() ; // create your endtime here, possibly using Calendar or Date
    long curTime = startDate.getTime();
    
    while (curTime <= endTime) {
        dates.add(new Date(curTime));
        curTime += interval;
    }
    for (int i = 0; i < dates.size(); i++){
        Date lDate = (Date)dates.get(i);
        String ds = formatter.format(lDate);    
        System.out.println("Date is ..." + ds);
        //Write your code for storing dates to list
    }
    

提交回复
热议问题