下面列出了java.time.LocalTime#isBefore ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test(dataProvider = "localTimeRanges")
public void testRangeOfLocalTimes(LocalTime start, LocalTime end, Duration step, boolean parallel) {
final Range<LocalTime> range = Range.of(start, end, step);
final Array<LocalTime> array = range.toArray(parallel);
final boolean ascend = start.isBefore(end);
final int expectedLength = (int)Math.ceil(Math.abs((double)ChronoUnit.SECONDS.between(start, end)) / (double)step.getSeconds());
Assert.assertEquals(array.length(), expectedLength);
Assert.assertEquals(array.typeCode(), ArrayType.LOCAL_TIME);
Assert.assertTrue(!array.style().isSparse());
Assert.assertEquals(range.start(), start, "The range start");
Assert.assertEquals(range.end(), end, "The range end");
LocalTime expected = null;
for (int i=0; i<array.length(); ++i) {
final LocalTime actual = array.getValue(i);
expected = expected == null ? start : ascend ? expected.plus(step) : expected.minus(step);
Assert.assertEquals(actual, expected, "Value matches at " + i);
Assert.assertTrue(ascend ? actual.compareTo(start) >=0 && actual.isBefore(end) : actual.compareTo(start) <= 0 && actual.isAfter(end), "Value in bounds at " + i);
}
}
@Test(dataProvider = "LocalTimeRanges")
public void testRangeOfLocalTimes(LocalTime start, LocalTime end, Duration step, boolean parallel) {
final boolean ascend = start.isBefore(end);
final Range<LocalTime> range = Range.of(start, end, step, v -> v.getHour() == 6);
final Array<LocalTime> array = range.toArray(parallel);
final LocalTime first = array.first(v -> true).map(ArrayValue::getValue).get();
final LocalTime last = array.last(v -> true).map(ArrayValue::getValue).get();
Assert.assertEquals(array.typeCode(), ArrayType.LOCAL_TIME);
Assert.assertTrue(!array.style().isSparse());
Assert.assertEquals(range.start(), start, "The range start");
Assert.assertEquals(range.end(), end, "The range end");
int index = 0;
LocalTime value = first;
while (ascend ? value.isBefore(last) : value.isAfter(last)) {
final LocalTime actual = array.getValue(index);
Assert.assertEquals(actual, value, "Value matches at " + index);
Assert.assertTrue(ascend ? actual.compareTo(start) >= 0 && actual.isBefore(end) : actual.compareTo(start) <= 0 && actual.isAfter(end), "Value in bounds at " + index);
value = ascend ? value.plus(step) : value.minus(step);
while (value.getHour() == 6) value = ascend ? value.plus(step) : value.minus(step);
index++;
}
}
TimeFilter(final LocalTime start, final LocalTime end, final ZoneId timeZone, final Result onMatch,
final Result onMismatch, LocalDate now) {
super(onMatch, onMismatch);
this.startTime = start;
this.endTime = end;
this.timeZone = timeZone;
this.start = ZonedDateTime.of(now, startTime, timeZone).withEarlierOffsetAtOverlap().toInstant().toEpochMilli();
long endMillis = ZonedDateTime.of(now, endTime, timeZone).withEarlierOffsetAtOverlap().toInstant().toEpochMilli();
if (end.isBefore(start)) {
// End time must be tomorrow.
endMillis += DAY_MS;
}
duration = startTime.isBefore(endTime) ? Duration.between(startTime, endTime).toMillis() :
Duration.between(startTime, endTime).plusHours(24).toMillis();
long difference = (endMillis - this.start) - duration;
if (difference != 0) {
// Handle switch from standard time to daylight time and daylight time to standard time.
endMillis -= difference;
}
this.end = endMillis;
}
private void validateSettings(UpdateServiceSettings settings) {
if (settings.isEnableAutoUpdate()) {
LocalTime fromTime = DateTimeUtility.toLocalTime(from);
LocalTime toTime = DateTimeUtility.toLocalTime(to);
if (!fromTime.isBefore(toTime)) {
throw new IllegalArgumentException("'From' must be before 'To'");
}
}
}
private boolean isTimeForAutoUpdate(LocalDateTime dateTime) {
if (!enableAutoUpdate) {
return false;
}
LocalTime time = dateTime.toLocalTime();
return DayOfWeek.from(dateTime) == dayForUpdate && time.isAfter(fromTime) && time.isBefore(toTime);
}
/**
* @return
*/
public static String getGreeting() {
final LocalTime now = LocalTime.now();
if (now.isAfter(LocalTime.of(12, 0))) {
return MessageSource.get("mail.good-evening");
} else if (now.isBefore(LocalTime.of(12, 0))) {
return MessageSource.get("mail.good-morning");
} else {
return MessageSource.get("mail.good-night");
}
}
@Override
public boolean isSatisfied(Map<String, Object> inputs) {
if (startTime == null || endTime == null) {
logger.warn("Time condition with id {} is not well configured: startTime={} endTime = {}", module.getId(),
startTime, endTime);
return false;
}
LocalTime currentTime = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
// If the current time equals the start time, the condition is always true.
if (currentTime.equals(startTime)) {
logger.debug("Time condition with id {} evaluated, that the current time {} equals the start time: {}",
module.getId(), currentTime, startTime);
return true;
}
// If the start time is before the end time, the condition will evaluate as true,
// if the current time is between the start time and the end time.
if (startTime.isBefore(endTime)) {
if (currentTime.isAfter(startTime) && currentTime.isBefore(endTime)) {
logger.debug("Time condition with id {} evaluated, that {} is between {} and {}.", module.getId(),
currentTime, startTime, endTime);
return true;
}
}
// If the start time is set after the end time, the time values wrap around the midnight mark.
// So if the start time is 19:00 and the end time is 07:00, the condition will be true from
// 19:00 to 23:59 and 00:00 to 07:00.
else if (currentTime.isAfter(LocalTime.MIDNIGHT) && currentTime.isBefore(endTime)
|| currentTime.isAfter(startTime) && currentTime.isBefore(LocalTime.MAX)) {
logger.debug("Time condition with id {} evaluated, that {} is between {} and {}, or between {} and {}.",
module.getId(), currentTime, LocalTime.MIDNIGHT, endTime, startTime,
LocalTime.MAX.truncatedTo(ChronoUnit.MINUTES));
return true;
}
// If none of these conditions apply false is returned.
return false;
}
@Override
public BigDecimal getWorkingDayValueInHours(
WeeklyPlanning weeklyPlanning, LocalDate date, LocalTime from, LocalTime to) {
double value = 0;
DayPlanning dayPlanning = this.findDayPlanning(weeklyPlanning, date);
if (dayPlanning == null) {
return BigDecimal.valueOf(value);
}
// Compute morning leave duration
LocalTime morningFrom = dayPlanning.getMorningFrom();
LocalTime morningTo = dayPlanning.getMorningTo();
if (morningFrom != null && morningTo != null) {
LocalTime morningBegin = from != null && from.isAfter(morningFrom) ? from : morningFrom;
LocalTime morningEnd = to != null && to.isBefore(morningTo) ? to : morningTo;
if (to != null && to.isBefore(morningBegin)) {
return BigDecimal.ZERO;
} else if (from == null || from.isBefore(morningEnd)) {
value += ChronoUnit.MINUTES.between(morningBegin, morningEnd);
}
}
// Compute afternoon leave duration
LocalTime afternoonFrom = dayPlanning.getAfternoonFrom();
LocalTime afternoonTo = dayPlanning.getAfternoonTo();
if (afternoonFrom != null && afternoonTo != null) {
LocalTime afternoonBegin = from != null && from.isAfter(afternoonFrom) ? from : afternoonFrom;
LocalTime afternoonEnd = to != null && to.isBefore(afternoonTo) ? to : afternoonTo;
if (from != null && from.isAfter(afternoonEnd)) {
return BigDecimal.ZERO;
} else if (to == null || to.isAfter(afternoonBegin)) {
value += ChronoUnit.MINUTES.between(afternoonBegin, afternoonEnd);
}
}
return BigDecimal.valueOf(value).divide(BigDecimal.valueOf(60), BigDecimal.ROUND_HALF_UP);
}
@Override
public boolean isSatisfied(Map<String, Object> inputs) {
if (startTime == null || endTime == null) {
logger.warn("Time condition with id {} is not well configured: startTime={} endTime = {}", module.getId(),
startTime, endTime);
return false;
}
LocalTime currentTime = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
// If the current time equals the start time, the condition is always true.
if (currentTime.equals(startTime)) {
logger.debug("Time condition with id {} evaluated, that the current time {} equals the start time: {}",
module.getId(), currentTime, startTime);
return true;
}
// If the start time is before the end time, the condition will evaluate as true,
// if the current time is between the start time and the end time.
if (startTime.isBefore(endTime)) {
if (currentTime.isAfter(startTime) && currentTime.isBefore(endTime)) {
logger.debug("Time condition with id {} evaluated, that {} is between {} and {}.", module.getId(),
currentTime, startTime, endTime);
return true;
}
}
// If the start time is set after the end time, the time values wrap around the midnight mark.
// So if the start time is 19:00 and the end time is 07:00, the condition will be true from
// 19:00 to 23:59 and 00:00 to 07:00.
else if (currentTime.isAfter(LocalTime.MIDNIGHT) && currentTime.isBefore(endTime)
|| currentTime.isAfter(startTime) && currentTime.isBefore(LocalTime.MAX)) {
logger.debug("Time condition with id {} evaluated, that {} is between {} and {}, or between {} and {}.",
module.getId(), currentTime, LocalTime.MIDNIGHT, endTime, startTime,
LocalTime.MAX.truncatedTo(ChronoUnit.MINUTES));
return true;
}
// If none of these conditions apply false is returned.
return false;
}
public boolean isLeaveEarly(LocalTime punchedTime) {
return punchedTime.isBefore(endWork.minusMinutes(allowableLeaveEarlyMinutes));
}
public static boolean isDateEarly(LocalTime localTime1,LocalTime localTime2){
return localTime1.isBefore(localTime2);
}
@Nonnull
public static LocalTime getMin (@Nonnull final LocalTime aTime1, @Nonnull final LocalTime aTime2)
{
return aTime1.isBefore (aTime2) ? aTime1 : aTime2;
}
/**
* 判断时间是否在指定的时间段之类.
*
* @param date 需要判断的时间
* @param start 时间段的起始时间
* @param end 时间段的截止时间
* @return true or false
*/
public static boolean isBetween(final LocalTime date, final LocalTime start, final LocalTime end) {
if (date == null || start == null || end == null) {
throw new IllegalArgumentException("日期不能为空");
}
return date.isAfter(start) && date.isBefore(end);
}
/**
* 判断当前时间是否在指定时间范围
*
* @param from 开始时间
* @param to 结束时间
* @return 结果
*/
public static boolean between(LocalTime from, LocalTime to) {
LocalTime now = LocalTime.now();
return now.isAfter(from) && now.isBefore(to);
}
/**
* Returns true if the given time is within the range between
* section.getStart() and section.getStop()
* @param VALUE
* @return true if the given time is within the range of the section
*/
public boolean contains(final LocalTime VALUE) {
return VALUE.isAfter(getStart()) && VALUE.isBefore(getStop());
}
/**
* Checks that the value specified is in the bounds of this range
* @param value the value to check if in bounds
* @return true if in bounds
*/
private boolean inBounds(LocalTime value) {
return ascend ? value.compareTo(start()) >=0 && value.isBefore(end()) : value.compareTo(start()) <=0 && value.isAfter(end());
}
/**
* Returns true if the given time is within the range between
* section.getStart() and section.getStop()
* @param VALUE
* @return true if the given time is within the range of the section
*/
public boolean contains(final LocalTime VALUE) {
return VALUE.isAfter(getStart()) && VALUE.isBefore(getStop());
}
/**
* Returns true if the given time is within the range between
* section.getStart() and section.getStop()
* @param VALUE
* @return true if the given time is within the range of the section
*/
public boolean contains(final LocalTime VALUE) {
return VALUE.isAfter(getStart()) && VALUE.isBefore(getStop());
}