我正在处理休假申请。我需要一份两次约会之间的所有日期的清单。但我做不到。
结账,如果这对您有帮助的话:
import java.time.LocalDate;
import java.util.LinkedList;
import java.util.List;
public class DateManager {
public DateManager() {
}
public List<LocalDate> getLocalDateListBetween(LocalDate date1, LocalDate date2) {
List<LocalDate> list = new LinkedList<>();
//return empty list, if same days
if(date1.isEqual(date2)) return list;
//swap days if wrong order
if(date1.isAfter(date2)) {
LocalDate temp = date1;
date1 = date2;
date2 = temp;
}
list.add(date1);//remove this, if you don't want first Date to be in List
while(!date1.isEqual(date2)) {
date1 = date1.plusDays(1);
list.add(date1);
}
return list;
}
}
从任何地方打电话:
public static void main(String[] args) {
//localdate: year-month-day
LocalDate date1 = LocalDate.parse("2021-05-05");
LocalDate date2 = LocalDate.parse("2021-05-10");
DateManager manager = new DateManager();
System.out.println(manager.getLocalDateListBetween(date1, date2));
}