Using JodaTime...
LocalDate startDate = new LocalDate(2011, 11, 8);
LocalDate endDate = new LocalDate(2012, 5, 1);
startDate = startDate.withDayOfMonth(1);
while (!startDate.isAfter(endDate)) {
System.out.println("> " + startDate);
startDate = startDate.plusMonths(1);
LocalDate endOfMonth = startDate.minusDays(1);
System.out.println("< " + endOfMonth);
}
Using Java 8's time
API
LocalDate startDate = LocalDate.of(2011, 11, 8);
LocalDate endDate = LocalDate.of(2012, 5, 1);
startDate = startDate.withDayOfMonth(1);
while (!startDate.isAfter(endDate)) {
System.out.println("> " + startDate);
startDate = startDate.plusMonths(1);
LocalDate endOfMonth = startDate.minusDays(1);
System.out.println("< " + endOfMonth);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…