嗨我需要在给定之间获得每个月的所有开始日期和结束日期 两年
pulbic void printStartDateAndEndDate(Date start, Date end){
for(// start - end){
Sysout("1 st month starting date: "+ startDateOfMonth+ " End Date"+endDateOfMonth);
}
}
如果有人知道怎么做,请告诉我。我需要将“startDateOfMonth”和“endDateOfMonth”放在Date对象中。
答案 0 :(得分:2)
使用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);
}
使用Java 8的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);
}