java8的时间段比较处理工具类TimeUtils
import org.jooq.lambda.tuple.Tuple2;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
public class TimeUtils {
public static Boolean dateTimeInRange(LocalDateTime dateTime, Optional<LocalDateTime> dateTimeFrom, Optional<LocalDateTime> dateTimeTo, boolean includeHead, boolean includeLast) {
if (Objects.isNull(dateTime)) {
return false;
}
return dateTimeFrom.map(from -> includeHead ? !dateTime.isBefore(from) : dateTime.isAfter(from)).orElse(true) && dateTimeTo.map(to -> includeLast ? !dateTime.isAfter(to) : dateTime.isBefore(to)).orElse(true);
}
public static Boolean dateInRange(LocalDate date, Optional<LocalDate> dateFrom, Optional<LocalDate> dateTo, boolean includeHead, boolean includeLast) {
if (Objects.isNull(date)) {
return false;
}
return dateFrom.map(from -> includeHead ? !date.isBefore(from) : date.isAfter(from)).orElse(true) && dateTo.map(to -> includeLast ? !date.isAfter(to) : date.isBefore(to)).orElse(true);
}
public static Boolean timeInRange(LocalTime time, Optional<LocalTime> timeFrom, Optional<LocalTime> timeTo, boolean includeHead, boolean includeLast) {
if (Objects.isNull(time)) {
return false;
}
return timeFrom.map(from -> includeHead ? !time.isBefore(from) : time.isAfter(from)).orElse(true) && timeTo.map(to -> includeLast ? !time.isAfter(to) : time.isBefore(to)).orElse(true);
}
public static Boolean dateTimeInRange(LocalDateTime dateTime, Optional<LocalDateTime> dateTimeFrom, Optional<LocalDateTime> dateTimeTo) {
return dateTimeInRange(dateTime, dateTimeFrom, dateTimeTo, true, true);
}
public static Boolean dateInRange(LocalDate date, Optional<LocalDate> dateFrom, Optional<LocalDate> dateTo) {
return dateInRange(date, dateFrom, dateTo, true, true);
}
public static Boolean timeInRange(LocalTime time, Optional<LocalTime> timeFrom, Optional<LocalTime> timeTo) {
return timeInRange(time, timeFrom, timeTo, true, true);
}
public static Tuple2<Optional<LocalDateTime>, Optional<LocalDateTime>> dateTimeRange(List<Tuple2<Optional<LocalDateTime>, Optional<LocalDateTime>>> dateTimeList) {
if (dateTimeList.isEmpty()) {
throw new RuntimeException("dateTimeList is empty");
}
return dateTimeList.stream().reduce(new Tuple2<>(null, null), (z, e) -> {
Optional<LocalDateTime> from0 = z.v1();
Optional<LocalDateTime> to0 = z.v2();
final Optional<LocalDateTime> from;
final Optional<LocalDateTime> to;
if (Objects.nonNull(from0)) {
from = from0.flatMap(fromTime -> e.v1().flatMap(f -> fromTime.isBefore(f) ? Optional.of(fromTime) : Optional.of(f)));
}
else {
from = e.v1();
}
if (Objects.nonNull(to0)) {
to = to0.flatMap(toTime -> e.v2().flatMap(t -> toTime.isAfter(t) ? Optional.of(toTime) : Optional.of(t)));
}
else {
to = e.v2();
}
return new Tuple2<>(from, to);
}, (l, r) -> null);
}
public static Tuple2<Optional<LocalDate>, Optional<LocalDate>> dateRange(List<Tuple2<Optional<LocalDate>, Optional<LocalDate>>> dateList) {
if (dateList.isEmpty()) {
throw new RuntimeException("dateList is empty");
}
return dateList.stream().reduce(new Tuple2<>(null, null), (z, e) -> {
Optional<LocalDate> from0 = z.v1();
Optional<LocalDate> to0 = z.v2();
final Optional<LocalDate> from;
final Optional<LocalDate> to;
if (Objects.nonNull(from0)) {
from = from0.flatMap(fromTime -> e.v1().flatMap(f -> fromTime.isBefore(f) ? Optional.of(fromTime) : Optional.of(f)));
}
else {
from = e.v1();
}
if (Objects.nonNull(to0)) {
to = to0.flatMap(toTime -> e.v2().flatMap(t -> toTime.isAfter(t) ? Optional.of(toTime) : Optional.of(t)));
}
else {
to = e.v2();
}
return new Tuple2<>(from, to);
}, (l, r) -> null);
}
}
不积跬步无以至千里,不积小流无以成江海