Weekend filter for Java 8 LocalDateTime

前端 未结 6 1209
春和景丽
春和景丽 2021-02-20 03:47

I want to write a boolean valued function which returns true if the given LocalDateTime falls between two specific points in time, false otherwise.

Specific

6条回答
  •  故里飘歌
    2021-02-20 04:24

    An alternate Java 8+ solution would be to use a Predicate to test whether the date falls on a weekend.

    Predicate isWeekend = date -> DayOfWeek.from(date).get(ChronoField.DAY_OF_WEEK) > 5;
    

    Then you can use apply it in a stream like

    someListOfDates.stream()
       .filter(isWeekend)
       .forEach(System.out::println);
    

    No external dependencies needed. (Though, please use a logger in production.)

提交回复
热议问题