java8 根据开始时间-结束时间计算中间间隔日期

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class Test {

    public static List findDates(String start, String end){
        List timeList = new ArrayList<>();
        LocalDate startDate = LocalDate.parse(start);
        LocalDate endDate = LocalDate.parse(end);

        long distance = ChronoUnit.DAYS.between(startDate, endDate);
        if (distance < 1) {
            return timeList;
        }
        Stream.iterate(startDate, d -> {
            return d.plusDays(1);
        }).limit(distance + 1).forEach(f -> {
            timeList.add(f.toString());
        });
        return timeList;
    }

    public static void main(String[] args) {
        String stime = "2019-05-25";
        String etime = "2019-06-01";
        List list = findDates(stime, etime);
        for(String time : list) {
            System.out.println(time);
        }
    }


}

 

你可能感兴趣的:(java)