public class TestYearMonth {
public static void main(String[] args) {
int start = parse("2014-08");
int end = parse("2015-02");
for (int i = start + 1; i < end; i++) {
System.out.println(String.format("%04d-%02d", i / 12, i % 12 + 1));
}
}
private static int parse(String string) {
return Integer.valueOf(string.substring(0, 4)) * 12 + Integer.valueOf(string.substring(5)) - 1;
}
}
private void dumpMonths(String start, String end)
throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
Date startDate = format.parse(start);
Date endDate = format.parse(end);
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
Date date = startDate;
while (true) {
cal.add(Calendar.MONTH, 1);
date = cal.getTime();
if (date.equals(endDate)) {
break;
}
System.out.println(format.format(date));
}
}
Open in new window