Java Code Examples for java.time.Instant#minus()

The following examples show how to use java.time.Instant#minus() . These examples are extracted from open source projects. 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 Project: phoebus   File: TimeAxis.java    License: Eclipse Public License 1.0 5 votes vote down vote up
/** Create axis with label and listener. */
public static TimeAxis forDuration(final String name, final PlotPartListener listener,
        final Duration duration)
{
    final Instant end = Instant.now();
    final Instant start = end.minus(duration);
    return new TimeAxis(name, listener, start, end);
}
 
Example 2
/**
 * Compute the order in which candidates should be emitted.
 *
 * @param candidates all candidates to be emitted
 * @param <T>        type of items in the batch
 * @param <I>        type of the index of each batch
 * @return a <tt>Queue</tt> representing the order in which candidates should be emitted
 */
@Override
public <T extends Batchable<?>, I> Queue<Batch<T, I>> compute(Stream<Batch<T, I>> candidates) {
    // TODO: break too large candidates by a configurable maxBatchSize

    final Instant now = Instant.ofEpochMilli(scheduler.now());
    final Instant cutLine = now.minus(minimumTimeInQueueMs, ChronoUnit.MILLIS);
    final Stream<Batch<T, I>> inQueueMinimumRequired = candidates.filter(
            batch -> !batch.getOldestItemTimestamp().isAfter(cutLine)
    );

    PriorityQueue<Batch<T, I>> queue = new PriorityQueue<>(this::compare);
    inQueueMinimumRequired.forEach(queue::offer);
    return queue;
}
 
Example 3
public static Instant minus(Instant instant, Duration sub) {
    if(isInfFuture(instant)) {
        return INF_FUTURE;
    } else if(isInfPast(instant)) {
        return INF_PAST;
    } else if(isInfPositive(sub)) {
        return INF_PAST;
    } else if(isInfNegative(sub)) {
        return INF_FUTURE;
    } else {
        return instant.minus(sub);
    }
}
 
Example 4
@Test(expectedExceptions=DateTimeException.class)
public void minus_longTemporalUnit_overflowTooBig() {
    Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999);
    i.minus(999999999, NANOS);
    i.minus(-1, SECONDS);
}
 
Example 5
@Test(expectedExceptions=DateTimeException.class)
public void minus_longTemporalUnit_overflowTooSmall() {
    Instant i = Instant.ofEpochSecond(MIN_SECOND);
    i.minus(1, NANOS);
}
 
Example 6
@Test(expectedExceptions=DateTimeException.class)
public void minus_longTemporalUnit_overflowTooBig() {
    Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999);
    i.minus(999999999, NANOS);
    i.minus(-1, SECONDS);
}
 
Example 7
@Test(expectedExceptions=DateTimeException.class)
public void minus_longTemporalUnit_overflowTooSmall() {
    Instant i = Instant.ofEpochSecond(MIN_SECOND);
    i.minus(1, NANOS);
}
 
Example 8
@Test(expectedExceptions=DateTimeException.class)
public void minus_longTemporalUnit_overflowTooBig() {
    Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999);
    i.minus(999999999, NANOS);
    i.minus(-1, SECONDS);
}
 
Example 9
@Test
@Deployment(resources = { "org/flowable/engine/test/api/oneTaskProcess.bpmn20.xml" })
public void testQueryInstantVariable() throws Exception {
    Map<String, Object> vars = new HashMap<>();
    Instant instant1 = Instant.now();
    vars.put("instantVar", instant1);

    ProcessInstance processInstance1 = runtimeService.startProcessInstanceByKey("oneTaskProcess", vars);

    Instant instant2 = instant1.plusSeconds(1);
    vars = new HashMap<>();
    vars.put("instantVar", instant1);
    vars.put("instantVar2", instant2);
    ProcessInstance processInstance2 = runtimeService.startProcessInstanceByKey("oneTaskProcess", vars);

    Instant nextYear = instant1.plus(365, ChronoUnit.DAYS);
    vars = new HashMap<>();
    vars.put("instantVar", nextYear);
    ProcessInstance processInstance3 = runtimeService.startProcessInstanceByKey("oneTaskProcess", vars);

    Instant nextMonth = instant1.plus(30, ChronoUnit.DAYS);

    Instant twoYearsLater = instant1.plus(730, ChronoUnit.DAYS);

    Instant oneYearAgo = instant1.minus(365, ChronoUnit.DAYS);

    // Query on single instant variable, should result in 2 matches
    ExecutionQuery query = runtimeService.createExecutionQuery().variableValueEquals("instantVar", instant1);
    List<Execution> executions = query.list();
    assertThat(executions).hasSize(2);

    // Query on two instant variables, should result in single value
    query = runtimeService.createExecutionQuery().variableValueEquals("instantVar", instant1).variableValueEquals("instantVar2", instant2);
    Execution execution = query.singleResult();
    assertThat(execution).isNotNull();
    assertThat(execution.getId()).isEqualTo(processInstance2.getId());

    // Query with unexisting variable value
    execution = runtimeService.createExecutionQuery().variableValueEquals("instantVar", instant1.minus(1, ChronoUnit.HOURS)).singleResult();
    assertThat(execution).isNull();

    // Test NOT_EQUALS
    execution = runtimeService.createExecutionQuery().variableValueNotEquals("instantVar", instant1).singleResult();
    assertThat(execution).isNotNull();
    assertThat(execution.getId()).isEqualTo(processInstance3.getId());

    // Test GREATER_THAN
    execution = runtimeService.createExecutionQuery().variableValueGreaterThan("instantVar", nextMonth).singleResult();
    assertThat(execution).isNotNull();
    assertThat(execution.getId()).isEqualTo(processInstance3.getId());

    assertThat(runtimeService.createExecutionQuery().variableValueGreaterThan("instantVar", nextYear).count()).isZero();
    assertThat(runtimeService.createExecutionQuery().variableValueGreaterThan("instantVar", oneYearAgo).count()).isEqualTo(3);

    // Test GREATER_THAN_OR_EQUAL
    execution = runtimeService.createExecutionQuery().variableValueGreaterThanOrEqual("instantVar", nextMonth).singleResult();
    assertThat(execution).isNotNull();
    assertThat(execution.getId()).isEqualTo(processInstance3.getId());

    execution = runtimeService.createExecutionQuery().variableValueGreaterThanOrEqual("instantVar", nextYear).singleResult();
    assertThat(execution).isNotNull();
    assertThat(execution.getId()).isEqualTo(processInstance3.getId());

    assertThat(runtimeService.createExecutionQuery().variableValueGreaterThanOrEqual("instantVar", oneYearAgo).count()).isEqualTo(3);

    // Test LESS_THAN
    executions = runtimeService.createExecutionQuery().variableValueLessThan("instantVar", nextYear).list();
    assertThat(executions)
        .extracting(Execution::getId)
        .containsExactlyInAnyOrder(
            processInstance1.getId(),
            processInstance2.getId()
        );

    assertThat(runtimeService.createExecutionQuery().variableValueLessThan("instantVar", instant1).count()).isZero();
    assertThat(runtimeService.createExecutionQuery().variableValueLessThan("instantVar", twoYearsLater).count()).isEqualTo(3);

    // Test LESS_THAN_OR_EQUAL
    executions = runtimeService.createExecutionQuery().variableValueLessThanOrEqual("instantVar", nextYear).list();
    assertThat(executions).hasSize(3);

    assertThat(runtimeService.createExecutionQuery().variableValueLessThanOrEqual("instantVar", oneYearAgo).count()).isZero();

    // Test value-only matching
    execution = runtimeService.createExecutionQuery().variableValueEquals(nextYear).singleResult();
    assertThat(execution).isNotNull();
    assertThat(execution.getId()).isEqualTo(processInstance3.getId());

    executions = runtimeService.createExecutionQuery().variableValueEquals(instant1).list();
    assertThat(executions)
        .extracting(Execution::getId)
        .containsExactlyInAnyOrder(
            processInstance1.getId(),
            processInstance2.getId()
        );

    execution = runtimeService.createExecutionQuery().variableValueEquals(twoYearsLater).singleResult();
    assertThat(execution).isNull();
}
 
Example 10
Source Project: j2objc   File: TCKInstant.java    License: Apache License 2.0 4 votes vote down vote up
@Test(expected=DateTimeException.class)
public void minus_longTemporalUnit_overflowTooBig() {
    Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999);
    i.minus(999999999, NANOS);
    i.minus(-1, SECONDS);
}
 
Example 11
Source Project: diirt   File: TimestampTest.java    License: MIT License 4 votes vote down vote up
@Test
public void minus1() {
    Instant time = Instant.ofEpochSecond(0, 0);
    Instant newTime = time.minus(Duration.ofMillis(100));
    assertThat(newTime, equalTo(Instant.ofEpochSecond(-1, 900000000)));
}
 
Example 12
@Test(expectedExceptions=DateTimeException.class)
public void minus_Duration_overflowTooSmall() {
    Instant i = Instant.ofEpochSecond(MIN_SECOND);
    i.minus(Duration.ofSeconds(0, 1));
}
 
Example 13
@Test(expectedExceptions=DateTimeException.class)
public void minus_Duration_overflowTooBig() {
    Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999);
    i.minus(Duration.ofSeconds(-1, 999999999));
}
 
Example 14
@Test(expectedExceptions=DateTimeException.class)
public void minus_Duration_overflowTooBig() {
    Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999);
    i.minus(Duration.ofSeconds(-1, 999999999));
}
 
Example 15
@Test(expectedExceptions=DateTimeException.class)
public void minus_longTemporalUnit_overflowTooBig() {
    Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999);
    i.minus(999999999, NANOS);
    i.minus(-1, SECONDS);
}
 
Example 16
@Test(expectedExceptions=DateTimeException.class)
public void minus_longTemporalUnit_overflowTooSmall() {
    Instant i = Instant.ofEpochSecond(MIN_SECOND);
    i.minus(1, NANOS);
}
 
Example 17
Source Project: phoebus   File: TimestampTest.java    License: Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void minus2() {
    Instant time = Instant.ofEpochSecond(0, 0);
    Instant newTime = time.minus(Duration.ofMillis(100));
    assertThat(newTime, equalTo(Instant.ofEpochSecond(-1, 900000000)));
}
 
Example 18
Source Project: hottub   File: TCKInstant.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void minus_Duration_overflowTooSmall() {
    Instant i = Instant.ofEpochSecond(MIN_SECOND);
    i.minus(Duration.ofSeconds(0, 1));
}
 
Example 19
@Test(expectedExceptions=DateTimeException.class)
public void minus_longTemporalUnit_overflowTooSmall() {
    Instant i = Instant.ofEpochSecond(MIN_SECOND);
    i.minus(1, NANOS);
}
 
Example 20
Source Project: flo   File: FloRunnerTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void tasksRunInProcesses() throws Exception {

  final Instant today = Instant.now().truncatedTo(ChronoUnit.DAYS);
  final Instant yesterday = today.minus(1, ChronoUnit.DAYS);

  final Task<String> baz = Task.named("baz", today).ofType(String.class)
      .process(() -> {
        final String bazJvm = jvmName();
        log.info("baz: bazJvm={}, today={}", bazJvm, today);
        return bazJvm;
      });

  final Task<String[]> foo = Task.named("foo", yesterday).ofType(String[].class)
      .input(() -> baz)
      .process(bazJvm -> {
        final String fooJvm = jvmName();
        log.info("foo: fooJvm={}, bazJvm={}, yesterday={}", fooJvm, bazJvm, yesterday);
        return new String[]{bazJvm, fooJvm};
      });

  final Task<String> quux = Task.named("quux", today).ofType(String.class)
      .process(() -> {
        final String quuxJvm = jvmName();
        log.info("quux: quuxJvm={}, yesterday={}", quuxJvm, yesterday);
        return quuxJvm;
      });

  final Task<String[]> bar = Task.named("bar", today, yesterday).ofType(String[].class)
      .input(() -> foo)
      .input(() -> quux)
      .process((bazFooJvms, quuxJvm) -> {
        final String barJvm = jvmName();
        log.info("bar: barJvm={}, bazFooJvms={}, quuxJvm={} today={}, yesterday={}",
            barJvm, bazFooJvms, quuxJvm, today, yesterday);
        return Stream.concat(
            Stream.of(barJvm),
            Stream.concat(
                Stream.of(bazFooJvms),
                Stream.of(quuxJvm))
        ).toArray(String[]::new);
      });

  final List<String> jvms = Arrays.asList(runTask(bar).value());

  final String mainJvm = jvmName();

  log.info("main jvm: {}", mainJvm);
  log.info("task jvms: {}", jvms);
  final Set<String> uniqueJvms = new HashSet<>(jvms);
  assertThat(uniqueJvms.size(), is(4));
  assertThat(uniqueJvms, not(contains(mainJvm)));
}