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

The following examples show how to use java.time.LocalDateTime#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: CaseInstanceVariableResourceTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@CmmnDeployment(resources = { "org/flowable/cmmn/rest/service/api/repository/oneHumanTaskCase.cmmn" })
public void testGetCaseInstanceLocalDateTimeVariable() throws Exception {

    CaseInstance caseInstance = runtimeService.createCaseInstanceBuilder().caseDefinitionKey("oneHumanTaskCase").start();
    LocalDateTime now = LocalDateTime.now();
    LocalDateTime nowWithoutNanos = now.truncatedTo(ChronoUnit.MILLIS);
    runtimeService.setVariable(caseInstance.getId(), "variable", now);

    CloseableHttpResponse response = executeRequest(
            new HttpGet(SERVER_URL_PREFIX + CmmnRestUrls.createRelativeResourceUrl(CmmnRestUrls.URL_CASE_INSTANCE_VARIABLE,
                    caseInstance.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: 'localDateTime',"
                    + "  value: '" + nowWithoutNanos.toString() + "'"
                    + "}");
}
 
Example 2
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 testGetProcessInstanceLocalDateTimeVariable() throws Exception {

    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess");
    LocalDateTime now = LocalDateTime.now();
    LocalDateTime 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: 'localDateTime',"
                    + "  value: '" + nowWithoutNanos.toString() + "'"
                    + "}");
}
 
Example 3
Source File: TimeHelper.java    From teammates with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a copy of the {@link LocalDateTime} adjusted to be compatible with the format output by
 * {@link #parseDateTimeFromSessionsForm}, i.e. either the time is 23:59, or the minute is 0 and the hour is not 0.
 * The date time is first rounded to the nearest hour, then the special case 00:00 is handled.
 * @param ldt The {@link LocalDateTime} to be adjusted for compatibility.
 * @return a copy of {@code ldt} adjusted for compatibility, or null if {@code ldt} is null.
 * @see #parseDateTimeFromSessionsForm
 */
public static LocalDateTime adjustLocalDateTimeForSessionsFormInputs(LocalDateTime ldt) {
    if (ldt == null) {
        return null;
    }
    if (ldt.getMinute() == 0 && ldt.getHour() != 0 || ldt.getMinute() == 59 && ldt.getHour() == 23) {
        return ldt;
    }

    // Round to the nearest hour
    LocalDateTime rounded;
    LocalDateTime floor = ldt.truncatedTo(ChronoUnit.HOURS);
    LocalDateTime ceiling = floor.plusHours(1);
    Duration distanceToCeiling = Duration.between(ldt, ceiling);
    if (distanceToCeiling.compareTo(Duration.ofMinutes(30)) <= 0) {
        rounded = ceiling;
    } else {
        rounded = floor;
    }

    // Adjust 00:00 -> 23:59
    if (rounded.getHour() == 0) {
        return rounded.minusMinutes(1);
    }
    return rounded;
}
 
Example 4
Source File: DateUtil.java    From java-trader with Apache License 2.0 5 votes vote down vote up
/**
 * 规整时间的秒
 */
public static LocalDateTime round(LocalDateTime ldt) {
    int seconds = ldt.get(ChronoField.SECOND_OF_MINUTE);
    if (seconds >= 59) {
        ldt = ldt.plus(1, ChronoUnit.SECONDS);
    }
    return ldt.truncatedTo(ChronoUnit.SECONDS);
}
 
Example 5
Source File: StandardDialect.java    From doma with Apache License 2.0 5 votes vote down vote up
@Override
public LocalDateTime roundDownTimePart(LocalDateTime localDateTime) {
  if (localDateTime == null) {
    return null;
  }
  return localDateTime.truncatedTo(ChronoUnit.DAYS);
}
 
Example 6
Source File: Product.java    From spring-data-mongodb-datatables with Apache License 2.0 2 votes vote down vote up
/**
 * Since JDK 9, LocalDateTime uses a precision of nanoseconds, while the BSON dates in MongoDB have a millisecond
 * precision, so we have to truncate it in order not to lose information.
 *
 * @see <a href="https://bugs.openjdk.java.net/browse/JDK-8068730">https://bugs.openjdk.java.net/browse/JDK-8068730</a>
 * @see <a href="http://bsonspec.org/spec.html">http://bsonspec.org/spec.html</a>
 */
private static LocalDateTime truncateToMillis(LocalDateTime dateTime) {
    return dateTime.truncatedTo(ChronoUnit.MILLIS);
}
 
Example 7
Source File: DateTimeExtensions.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a {@link java.time.LocalDateTime} with the time portion cleared.
 *
 * @param self a LocalDateTime
 * @return a LocalDateTime
 * @since 2.5.0
 */
public static LocalDateTime clearTime(final LocalDateTime self) {
    return self.truncatedTo(DAYS);
}