java 8 stream 获取天数

//获取月中的天数

LocalDate startDate = LocalDate.now(ZoneId.of(location.getTimezone()));
LocalDate endDate = startDate.plusDays(days);

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");

long distance = ChronoUnit.DAYS.between(startDate, endDate);
Map<String, Long> monthColumnSpan = Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1)
.collect(Collectors.groupingBy(day -> day.format(formatter) + day.getMonth().toString(), Collectors.counting()));

 

Map<String, Long> monthColumnSpanSorted = new LinkedHashMap<>();
monthColumnSpan.entrySet().stream().sorted(Map.Entry.comparingByKey())
.forEachOrdered(e -> monthColumnSpanSorted.put(e.getKey().substring(6) + " " + e.getKey().substring(0, 4), e.getValue()));

posted @ 2019-11-24 18:20  payzulla  阅读(534)  评论(0编辑  收藏  举报