Find missing dates between two dates

I got a list of dates like String for example date1 → '11 -11-2010 'and date2 → '12 -01-2011' I want to print all the dates between these two dates. I tried to work with cal.add () but could not set date1 to my cal .. if I do this, I get null p

+3
source share
1 answer

below code should do the trick for you.

    String date1 = "11-11-2010";
    String date2 = "12-01-2011";

    SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
    Calendar calendar1 = Calendar.getInstance();
    calendar1.setTime(format.parse(date1));

    Calendar calendar2 = Calendar.getInstance();
    calendar2.setTime(format.parse(date2));

    Date currentDate = calendar1.getTime();
    while(!currentDate.equals(cal2.getTime())){
        calendar1.add(Calendar.DAY_OF_MONTH, 1);
        currentDate = cal1.getTime();
        System.out.println(currentDate);

    }
+3
source

Source: https://habr.com/ru/post/1776162/


All Articles