I have two dates:
Start Date: 2017-01-15
End Date: 2017-05-27
Now I want to find the whole month between these two dates. So the result should be:
Feb-2017
Mar-2017
Apr-2017
January is not included in the result because start date starts from the middle of the month and May is not included because end date does not cover whole May month.
How can I achieve this with Java?
I have tried doing something like:
String date1 = "2015-01-15";
String date2 = "2015-05-27";
DateFormat formater1 = new SimpleDateFormat("MMM-yyyy");
DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
Calendar startCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance();
try {
startCalendar.setTime(formater.parse(date1));
endCalendar.setTime(formater.parse(date2));
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("startDate:" + startCalendar.getTime());
System.out.println("endCalendar:" + endCalendar.getTime());
while (startCalendar.before(endCalendar)) {
// add one month to date per loop
String date = formater1.format(startCalendar.getTime()).toUpperCase();
System.out.println(date);
startCalendar.add(Calendar.MONTH, 1);
}
But I don't know how to exclude Jan and May from the result. Currently, I am getting this result:
JAN-2015
FEB-2015
MAR-2015
APR-2015
MAY-2015