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

The following examples show how to use java.time.LocalTime#plus() . 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: 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 2
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 3
Source File: Java8TestPeriodDuration.java    From javase with MIT License 5 votes vote down vote up
public void testDuration(){
   LocalTime time1 = LocalTime.now();
   Duration twoHours = Duration.ofHours(2);
		
   LocalTime time2 = time1.plus(twoHours);
   Duration duration = Duration.between(time1, time2);
		
   System.out.println("Duration: " + duration);
}
 
Example 4
Source File: TimeOfDayConditionHandlerTest.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * This checks if the condition on its own works properly.
 */
@Test
public void assertThatConditionWorks() {
    LocalTime currentTime = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
    LocalTime beforeCurrentTime = currentTime.minus(Duration.ofMinutes(2));
    LocalTime afterCurrentTime = currentTime.plus(Duration.ofMinutes(2));

    // Time is between start and end time -> should return true.
    TimeOfDayConditionHandler handler = getTimeOfDayConditionHandler(beforeCurrentTime.toString(),
            afterCurrentTime.toString());
    assertThat(handler.isSatisfied(Collections.emptyMap()), is(true));

    // Time is equal to start time -> should return true
    handler = getTimeOfDayConditionHandler(currentTime.toString(), afterCurrentTime.toString());
    assertThat(handler.isSatisfied(Collections.emptyMap()), is(true));

    // Time is equal to end time -> should return false
    handler = getTimeOfDayConditionHandler(beforeCurrentTime.toString(), currentTime.toString());
    assertThat(handler.isSatisfied(Collections.emptyMap()), is(false));

    // Start value is in the future & end value is in the past
    // -> should return false
    handler = getTimeOfDayConditionHandler(afterCurrentTime.toString(), beforeCurrentTime.toString());
    assertThat(handler.isSatisfied(Collections.emptyMap()), is(false));

    // Start & end time are in the future & start time is after the end time
    // -> should return true
    handler = getTimeOfDayConditionHandler(afterCurrentTime.plus(Duration.ofMinutes(2)).toString(),
            afterCurrentTime.toString());
    assertThat(handler.isSatisfied(Collections.emptyMap()), is(true));
}
 
Example 5
Source File: TimeOfDayConditionHandlerTest.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Condition getPassingCondition() {
    LocalTime currentTime = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
    LocalTime beforeCurrentTime = currentTime.minus(Duration.ofMinutes(2));
    LocalTime afterCurrentTime = currentTime.plus(Duration.ofMinutes(2));
    return getTimeCondition(beforeCurrentTime.toString(), afterCurrentTime.toString());
}
 
Example 6
Source File: TimeOfDayConditionHandlerTest.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Configuration getFailingConfiguration() {
    LocalTime currentTime = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
    LocalTime beforeCurrentTime = currentTime.minus(Duration.ofMinutes(2));
    LocalTime afterCurrentTime = currentTime.plus(Duration.ofMinutes(2));
    return getTimeConfiguration(afterCurrentTime.toString(), beforeCurrentTime.toString());
}
 
Example 7
Source File: EdmTimeOfDayTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void valueOfStringToLocalTime() throws Exception {
  LocalTime time = LocalTime.parse("04:05:06");
  assertEquals(time, instance.valueOfString("04:05:06", null, null, null, null, null, LocalTime.class));

  time = time.plus(123, ChronoUnit.MILLIS);
  assertEquals(time, instance.valueOfString("04:05:06.123", null, null, null, null, null, LocalTime.class));

  time = time.plus(456789, ChronoUnit.NANOS);
  assertEquals(time, instance.valueOfString("04:05:06.123456789", null, null, null, null, null, LocalTime.class));
}
 
Example 8
Source File: TimeOfDayConditionHandlerTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * This checks if the condition on its own works properly.
 */
@Test
public void assertThatConditionWorks() {
    LocalTime currentTime = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
    LocalTime beforeCurrentTime = currentTime.minus(Duration.ofMinutes(2));
    LocalTime afterCurrentTime = currentTime.plus(Duration.ofMinutes(2));

    // Time is between start and end time -> should return true.
    TimeOfDayConditionHandler handler = getTimeOfDayConditionHandler(beforeCurrentTime.toString(),
            afterCurrentTime.toString());
    assertThat(handler.isSatisfied(null), is(true));

    // Time is equal to start time -> should return true
    handler = getTimeOfDayConditionHandler(currentTime.toString(), afterCurrentTime.toString());
    assertThat(handler.isSatisfied(null), is(true));

    // Time is equal to end time -> should return false
    handler = getTimeOfDayConditionHandler(beforeCurrentTime.toString(), currentTime.toString());
    assertThat(handler.isSatisfied(null), is(false));

    // Start value is in the future & end value is in the past
    // -> should return false
    handler = getTimeOfDayConditionHandler(afterCurrentTime.toString(), beforeCurrentTime.toString());
    assertThat(handler.isSatisfied(null), is(false));

    // Start & end time are in the future & start time is after the end time
    // -> should return true
    handler = getTimeOfDayConditionHandler(afterCurrentTime.plus(Duration.ofMinutes(2)).toString(),
            afterCurrentTime.toString());
    assertThat(handler.isSatisfied(null), is(true));
}
 
Example 9
Source File: TimeOfDayConditionHandlerTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Condition getPassingCondition() {
    LocalTime currentTime = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
    LocalTime beforeCurrentTime = currentTime.minus(Duration.ofMinutes(2));
    LocalTime afterCurrentTime = currentTime.plus(Duration.ofMinutes(2));
    return getTimeCondition(beforeCurrentTime.toString(), afterCurrentTime.toString());
}
 
Example 10
Source File: TimeOfDayConditionHandlerTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Configuration getFailingConfiguration() {
    LocalTime currentTime = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
    LocalTime beforeCurrentTime = currentTime.minus(Duration.ofMinutes(2));
    LocalTime afterCurrentTime = currentTime.plus(Duration.ofMinutes(2));
    return getTimeConfiguration(afterCurrentTime.toString(), beforeCurrentTime.toString());
}
 
Example 11
Source File: JavaDurationUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenATimePlus30Seconds_whenRequestingDuration_thenExpect30() {
    LocalTime initialTime = LocalTime.of(6, 30, 0);
    LocalTime finalTime = initialTime.plus(Duration.ofSeconds(30));

    long seconds = Duration.between(initialTime, finalTime)
        .getSeconds();

    assertThat(seconds).isEqualTo(30);
}
 
Example 12
Source File: JavaDurationUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenATimePlus30Seconds_whenRequestingSecondsBetween_thenExpect30() {
    LocalTime initialTime = LocalTime.of(6, 30, 0);
    LocalTime finalTime = initialTime.plus(Duration.ofSeconds(30));

    long seconds = ChronoUnit.SECONDS.between(initialTime, finalTime);

    assertThat(seconds).isEqualTo(30);
}
 
Example 13
Source File: UseDuration.java    From tutorials with MIT License 4 votes vote down vote up
public LocalTime modifyDates(LocalTime localTime, Duration duration) {
    return localTime.plus(duration);
}
 
Example 14
Source File: UseLocalTime.java    From tutorials with MIT License 4 votes vote down vote up
LocalTime addAnHour(LocalTime localTime) {
    return localTime.plus(1, ChronoUnit.HOURS);
}