营业时间判断?

twwvjkzc
• 7

5 个回答
✓ 已被采纳

`validateTime < endTime || validateTime > startTime`

`LocalTime`写成代码应该就类似如下：

``````LocalTime startTime = LocalTime.of(13, 0);
LocalTime endTime = LocalTime.of(3, 0);
LocalTime validateTime1 = LocalTime.of(16, 34);
LocalTime validateTime2 = LocalTime.of(3, 10);

Predicate<LocalTime> endBeforeStartPredicate = localTime -> localTime.isAfter(startTime) || localTime.isBefore(endTime);

System.out.println(endBeforeStartPredicate.test(validateTime1)); // true
System.out.println(endBeforeStartPredicate.test(validateTime2)); // false``````

``````LocalTime startTime = LocalTime.of(13, 0);
LocalTime endTime = LocalTime.of(3, 0);
LocalTime validateTime1 = LocalTime.of(16, 34);
LocalTime validateTime2 = LocalTime.of(3, 10);

BiFunction<LocalTime, LocalTime, Predicate<LocalTime>> biFunction = (start, end) ->
end.isAfter(start)
? localTime -> localTime.isAfter(start) && localTime.isBefore(end)
: localTime -> localTime.isAfter(start) || localTime.isBefore(end);

System.out.println(biFunction.apply(startTime, endTime).test(validateTime1)); // true
System.out.println(biFunction.apply(startTime, endTime).test(validateTime2)); // false

// startTime和endTime换一下位置，此时表示是当天的时间的比较，没有跨天
System.out.println(biFunction.apply(endTime, startTime).test(validateTime1)); // false
System.out.println(biFunction.apply(endTime, startTime).test(validateTime2)); // true
``````

`biFunction`这里的写法比较捡懒同时可以让代码放在一起，你可以选择整一个工具方法放着都行，感觉这样差不多叭~(￣(エ)￣)