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

The following examples show how to use java.time.Instant#truncatedTo() . 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: ProcessInstanceVariableResourceTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
@Deployment(resources = { "org/flowable/rest/service/api/runtime/ProcessInstanceVariableResourceTest.testProcess.bpmn20.xml" })
public void testGetProcessInstanceInstantVariable() throws Exception {

    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess");
    Instant now = Instant.now();
    Instant nowWithoutNanos = now.truncatedTo(ChronoUnit.MILLIS);
    runtimeService.setVariable(processInstance.getId(), "variable", now);

    CloseableHttpResponse response = executeRequest(
            new HttpGet(
                    SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_PROCESS_INSTANCE_VARIABLE, processInstance.getId(), "variable")),
            HttpStatus.SC_OK);

    JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());

    closeResponse(response);
    assertThat(responseNode).isNotNull();
    assertThatJson(responseNode)
            .when(Option.IGNORING_EXTRA_FIELDS)
            .isEqualTo("{"
                    + "  name: 'variable',"
                    + "  type: 'instant',"
                    + "  value: '" + nowWithoutNanos.toString() + "'"
                    + "}");
}
 
Example 2
Source File: AbstractMarketplaceAccessor.java    From robozonky with Apache License 2.0 6 votes vote down vote up
protected Select getIncrementalFilter() {
    Instant lastFullMarketplaceCheck = lastFullMarketplaceCheckReference.get();
    Instant now = DateUtil.now();
    if (lastFullMarketplaceCheck.plus(getForcedMarketplaceCheckInterval())
        .isBefore(now)) {
        Instant newTimestamp = now.truncatedTo(ChronoUnit.SECONDS); // Go a couple millis back.
        lastFullMarketplaceCheckReference.set(newTimestamp);
        getLogger().debug("Running full marketplace check with timestamp of {}, previous was {}.", newTimestamp,
                lastFullMarketplaceCheck);
        return getBaseFilter();
    } else {
        var filter = getBaseFilter()
            .greaterThanOrEquals("datePublished",
                    OffsetDateTime.ofInstant(lastFullMarketplaceCheck, Defaults.ZONE_ID));
        getLogger().debug("Running incremental marketplace check, starting from {}.", lastFullMarketplaceCheck);
        return filter;
    }
}
 
Example 3
Source File: InstantVariableTest.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Test
@Deployment(resources = "org/flowable/engine/test/api/oneTaskProcess.bpmn20.xml")
void testGetInstantVariable() {
    Instant nowInstant = Instant.now();
    Instant nowInstantWithoutNanos = nowInstant.truncatedTo(ChronoUnit.MILLIS);
    Instant oneYearBefore = nowInstant.minus(365, ChronoUnit.DAYS);
    Instant oneYearBeforeWithoutNanos = oneYearBefore.truncatedTo(ChronoUnit.MILLIS);
    Instant oneYearLater = nowInstant.plus(365, ChronoUnit.DAYS);
    Instant oneYearLaterWithoutNanos = oneYearLater.truncatedTo(ChronoUnit.MILLIS);
    ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder()
            .processDefinitionKey("oneTaskProcess")
            .variable("nowInstant", nowInstant)
            .variable("oneYearBefore", oneYearBefore)
            .variable("oneYearLater", oneYearLater)
            .start();

    VariableInstance nowInstantVariableInstance = runtimeService.getVariableInstance(processInstance.getId(), "nowInstant");
    assertThat(nowInstantVariableInstance.getTypeName()).isEqualTo(InstantType.TYPE_NAME);
    assertThat(nowInstantVariableInstance.getValue()).isEqualTo(nowInstantWithoutNanos);

    VariableInstance oneYearBeforeVariableInstance = runtimeService.getVariableInstance(processInstance.getId(), "oneYearBefore");
    assertThat(oneYearBeforeVariableInstance.getTypeName()).isEqualTo(InstantType.TYPE_NAME);
    assertThat(oneYearBeforeVariableInstance.getValue()).isEqualTo(oneYearBeforeWithoutNanos);

    VariableInstance oneYearLaterVariableInstance = runtimeService.getVariableInstance(processInstance.getId(), "oneYearLater");
    assertThat(oneYearLaterVariableInstance.getTypeName()).isEqualTo(InstantType.TYPE_NAME);
    assertThat(oneYearLaterVariableInstance.getValue()).isEqualTo(oneYearLaterWithoutNanos);

    assertThat(runtimeService.getVariables(processInstance.getId()))
            .containsOnly(
                    entry("nowInstant", nowInstantWithoutNanos),
                    entry("oneYearBefore", oneYearBeforeWithoutNanos),
                    entry("oneYearLater", oneYearLaterWithoutNanos)
            );
}
 
Example 4
Source File: ProcessInstanceVariableResourceTest.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Test
@Deployment(resources = { "org/flowable/rest/service/api/runtime/ProcessInstanceVariableResourceTest.testProcess.bpmn20.xml" })
public void testUpdateInstantProcessVariable() throws Exception {
    Instant initial = Instant.parse("2019-12-03T12:32:45.583345Z");
    Instant tenDaysLater = initial.plus(10, ChronoUnit.DAYS);
    Instant tenDaysLaterWithoutNanos = tenDaysLater.truncatedTo(ChronoUnit.MILLIS);
    ProcessInstance processInstance = runtimeService
            .startProcessInstanceByKey("oneTaskProcess", Collections.singletonMap("overlappingVariable", (Object) "processValue"));
    runtimeService.setVariable(processInstance.getId(), "myVar", initial);

    // Update variable
    ObjectNode requestNode = objectMapper.createObjectNode();
    requestNode.put("name", "myVar");
    requestNode.put("value", "2019-12-13T12:32:45.583345Z");
    requestNode.put("type", "instant");

    HttpPut httpPut = new HttpPut(
            SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_PROCESS_INSTANCE_VARIABLE, processInstance.getId(), "myVar"));
    httpPut.setEntity(new StringEntity(requestNode.toString()));
    CloseableHttpResponse response = executeRequest(httpPut, HttpStatus.SC_OK);

    assertThatJson(runtimeService.getVariable(processInstance.getId(), "myVar")).isEqualTo(tenDaysLaterWithoutNanos);

    JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
    closeResponse(response);
    assertThat(responseNode).isNotNull();
    assertThatJson(responseNode)
            .when(Option.IGNORING_EXTRA_FIELDS)
            .isEqualTo("{"
                    + "  value: '2019-12-13T12:32:45.583345Z'"
                    + "}");
}
 
Example 5
Source File: InstantVariableTest.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Test
@Deployment(resources = "org/flowable/engine/test/api/oneTaskProcess.bpmn20.xml")
void testGetInstantVariableFromTask() {
    ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder()
            .processDefinitionKey("oneTaskProcess")
            .start();

    Map<String, Object> variables = new HashMap<>();
    Instant nowInstant = Instant.now();
    Instant nowInstantWithoutNanos = nowInstant.truncatedTo(ChronoUnit.MILLIS);
    Instant oneYearLater = nowInstant.plus(365, ChronoUnit.DAYS);
    Instant oneYearLaterWithoutNanos = oneYearLater.truncatedTo(ChronoUnit.MILLIS);
    variables.put("nowInstant", nowInstant);
    variables.put("oneYearLater", oneYearLater);
    Task task = taskService.createTaskQuery().singleResult();
    taskService.setVariables(task.getId(), variables);

    VariableInstance nowInstantVariableInstance = taskService.getVariableInstance(task.getId(), "nowInstant");
    assertThat(nowInstantVariableInstance.getTypeName()).isEqualTo(InstantType.TYPE_NAME);
    assertThat(nowInstantVariableInstance.getValue()).isEqualTo(nowInstantWithoutNanos);

    VariableInstance oneYearLaterVariableInstance = taskService.getVariableInstance(task.getId(), "oneYearLater");
    assertThat(oneYearLaterVariableInstance.getTypeName()).isEqualTo(InstantType.TYPE_NAME);
    assertThat(oneYearLaterVariableInstance.getValue()).isEqualTo(oneYearLaterWithoutNanos);

    assertThat(taskService.getVariables(task.getId()))
            .containsOnly(
                    entry("nowInstant", nowInstantWithoutNanos),
                    entry("oneYearLater", oneYearLaterWithoutNanos)
            );
}
 
Example 6
Source File: RollingFileWriter.java    From swage with Apache License 2.0 5 votes vote down vote up
/**
 * If necessary, close the current logfile and open a new one,
 * updating {@link #writer}.
 */
private void rollIfNeeded() throws IOException {
    // Do not roll unless record is complete, as indicated by flush
    if (!flushed) return;
    flushed = false;

    // If we have not yet passed the roll over mark do nothing
    Instant now = Instant.now();
    if (now.isBefore(rollAt)) {
        return;
    }

    // New file time, may not be the rollAt time if one or more intervals
    // have passed without anything being written
    Instant rollTime = now.truncatedTo(rollInterval);

    // Determine the name of the file that will be written to
    String name = this.baseName + format.format(LocalDateTime.ofInstant(rollTime, timeZone));
    this.outPath = this.directory.resolve(name);

    // Finish writing to previous log
    if (writer != null) {
        writer.flush();
        writer.close();
    }

    // Ensure the parent directory always exists, even if it was removed out from under us.
    // A no-op if the directory already exists.
    Files.createDirectories(outPath.getParent());

    // Create new file, set it as our write destination
    writer = Files.newBufferedWriter(
                outPath,
                StandardCharsets.UTF_8,
                StandardOpenOption.CREATE,
                StandardOpenOption.APPEND);

    // Point to the next time we want to roll the log, update rollAt
    this.rollAt = rollTime.plus(1, rollInterval);
}
 
Example 7
Source File: TCKInstant.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) {
    input.truncatedTo(unit);
}
 
Example 8
Source File: TCKInstant.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test(expected=DateTimeException.class)
@UseDataProvider("data_truncatedToInvalid")
public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) {
    input.truncatedTo(unit);
}
 
Example 9
Source File: TCKInstant.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) {
    input.truncatedTo(unit);
}
 
Example 10
Source File: TCKInstant.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) {
    input.truncatedTo(unit);
}
 
Example 11
Source File: TCKInstant.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) {
    input.truncatedTo(unit);
}
 
Example 12
Source File: TCKInstant.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) {
    input.truncatedTo(unit);
}
 
Example 13
Source File: TCKInstant.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) {
    input.truncatedTo(unit);
}
 
Example 14
Source File: TCKInstant.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) {
    input.truncatedTo(unit);
}
 
Example 15
Source File: TCKInstant.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) {
    input.truncatedTo(unit);
}
 
Example 16
Source File: TCKInstant.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) {
    input.truncatedTo(unit);
}
 
Example 17
Source File: DateUtil.java    From adaptive-alerting with Apache License 2.0 4 votes vote down vote up
public static Instant truncatedToSeconds(Instant date) {
    notNull(date, "date can't be null");
    return date.truncatedTo(ChronoUnit.SECONDS);
}
 
Example 18
Source File: TCKInstant.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) {
    input.truncatedTo(unit);
}
 
Example 19
Source File: TCKInstant.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) {
    input.truncatedTo(unit);
}
 
Example 20
Source File: DateUtil.java    From adaptive-alerting with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the largest day either before or equal to the given date.
 *
 * @param date A date.
 * @return Largest day before or equal to the given date.
 */
public static Instant truncatedToDay(Instant date) {
    notNull(date, "date can't be null");
    return date.truncatedTo(ChronoUnit.DAYS);
}