Java Code Examples for java.time.LocalTime#isBefore()

The following examples show how to use java.time.LocalTime#isBefore() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RangeFilterTests.java    From morpheus-core with Apache License 2.0 6 votes vote down vote up
@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++;
    }
}
 
Example 2
Source File: TimeFilter.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: RangeBasicTests.java    From morpheus-core with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example 4
Source File: UpdaterConfigBean.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
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'");
        }
    }
}
 
Example 5
Source File: UpdateService.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
private boolean isTimeForAutoUpdate(LocalDateTime dateTime) {
    if (!enableAutoUpdate) {
        return false;
    }
    LocalTime time = dateTime.toLocalTime();
    return DayOfWeek.from(dateTime) == dayForUpdate && time.isAfter(fromTime) && time.isBefore(toTime);
}
 
Example 6
Source File: EmailUtil.java    From library with Apache License 2.0 5 votes vote down vote up
/**
 * @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");
    }
}
 
Example 7
Source File: TimeOfDayConditionHandler.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote 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;
}
 
Example 8
Source File: WeeklyPlanningServiceImp.java    From axelor-open-suite with GNU Affero General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example 9
Source File: TimeOfDayConditionHandler.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote 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;
}
 
Example 10
Source File: DateUtil.java    From JavaWeb with Apache License 2.0 4 votes vote down vote up
public static boolean isDateEarly(LocalTime localTime1,LocalTime localTime2){
	return localTime1.isBefore(localTime2);
}
 
Example 11
Source File: PDTHelper.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static LocalTime getMin (@Nonnull final LocalTime aTime1, @Nonnull final LocalTime aTime2)
{
  return aTime1.isBefore (aTime2) ? aTime1 : aTime2;
}
 
Example 12
Source File: WorkTimeRule.java    From eas-ddd with Apache License 2.0 4 votes vote down vote up
public boolean isLeaveEarly(LocalTime punchedTime) {
    return punchedTime.isBefore(endWork.minusMinutes(allowableLeaveEarlyMinutes));
}
 
Example 13
Source File: DateUtils.java    From Raincat with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * 判断时间是否在指定的时间段之类.
 *
 * @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);
}
 
Example 14
Source File: RangeOfLocalTimes.java    From morpheus-core with Apache License 2.0 2 votes vote down vote up
/**
 * 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());
}
 
Example 15
Source File: TimeSection.java    From OEE-Designer with MIT License 2 votes vote down vote up
/**
 * 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());
}
 
Example 16
Source File: DateUtil.java    From FEBS-Cloud with Apache License 2.0 2 votes vote down vote up
/**
 * 判断当前时间是否在指定时间范围
 *
 * @param from 开始时间
 * @param to   结束时间
 * @return 结果
 */
public static boolean between(LocalTime from, LocalTime to) {
    LocalTime now = LocalTime.now();
    return now.isAfter(from) && now.isBefore(to);
}
 
Example 17
Source File: TimeSection.java    From tilesfx with Apache License 2.0 2 votes vote down vote up
/**
 * 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());
}
 
Example 18
Source File: TimeSection.java    From Medusa with Apache License 2.0 2 votes vote down vote up
/**
 * 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());
}