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

The following examples show how to use java.time.LocalDateTime#minusYears() . 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: LocalDateTimeVariableTest.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 testGetLocalDateTimeVariable() {
    LocalDateTime nowLocalDateTime = LocalDateTime.now().truncatedTo(ChronoUnit.MILLIS);
    LocalDateTime oneYearBefore = nowLocalDateTime.minusYears(1);
    LocalDateTime oneYearLater = nowLocalDateTime.plusYears(1);
    ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder()
            .processDefinitionKey("oneTaskProcess")
            .variable("nowLocalDateTime", nowLocalDateTime)
            .variable("oneYearBefore", oneYearBefore)
            .variable("oneYearLater", oneYearLater)
            .start();

    VariableInstance nowLocalDateTimeVariableInstance = runtimeService.getVariableInstance(processInstance.getId(), "nowLocalDateTime");
    assertThat(nowLocalDateTimeVariableInstance.getTypeName()).isEqualTo(LocalDateTimeType.TYPE_NAME);
    assertThat(nowLocalDateTimeVariableInstance.getValue()).isEqualTo(nowLocalDateTime);

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

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

    assertThat(runtimeService.getVariables(processInstance.getId()))
            .containsOnly(
                    entry("nowLocalDateTime", nowLocalDateTime),
                    entry("oneYearBefore", oneYearBefore),
                    entry("oneYearLater", oneYearLater)
            );
}
 
Example 2
Source File: DefaultDateFunctions.java    From jackcess with Apache License 2.0 5 votes vote down vote up
private static int getDayDiff(LocalDateTime ldt1, LocalDateTime ldt2) {
  int y1 = ldt1.getYear();
  int d1 = ldt1.getDayOfYear();
  int y2 = ldt2.getYear();
  int d2 = ldt2.getDayOfYear();
  while(y2  > y1) {
    ldt2 = ldt2.minusYears(1);
    d2 += ldt2.range(ChronoField.DAY_OF_YEAR).getMaximum();
    y2 = ldt2.getYear();
  }
  return d2 - d1;
}
 
Example 3
Source File: ImportDateTime.java    From axelor-open-suite with GNU Affero General Public License v3.0 5 votes vote down vote up
public LocalDateTime updateYear(LocalDateTime datetime, String year) {
  if (!Strings.isNullOrEmpty(year)) {
    Matcher matcher = patternYear.matcher(year);
    if (matcher.find()) {
      Long years = Long.parseLong(matcher.group());
      if (year.startsWith("+")) datetime = datetime.plusYears(years);
      else if (year.startsWith("-")) datetime = datetime.minusYears(years);
      else datetime = datetime.withYear(years.intValue());
    }
  }
  return datetime;
}
 
Example 4
Source File: DateProducer.java    From jfairy with Apache License 2.0 5 votes vote down vote up
public LocalDateTime randomDateInThePast(int maxYearsEarlier) {
	checkArgument(maxYearsEarlier >= 0, "%s has to be >= 0", maxYearsEarlier);
	LocalDateTime currentDate = timeProvider.getCurrentTime();
	LocalDateTime latestDateInThePast = currentDate.minusSeconds(SECONDS_BEFORE_TO_BE_IN_THE_PAST);
	LocalDateTime maxYearsEarlierDate = currentDate.minusYears(maxYearsEarlier);
	return randomDateBetweenTwoDates(maxYearsEarlierDate, latestDateInThePast);
}
 
Example 5
Source File: DateMinusYears.java    From levelup-java-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void subtract_years_from_date_in_java8() {

	LocalDateTime newYearsDay = LocalDateTime.of(2011, Month.FEBRUARY, 6,
			0, 0);
	LocalDateTime numberFour = newYearsDay.minusYears(14);

	java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
			.ofPattern("MM/dd/yyyy HH:mm:ss S");

	logger.info(newYearsDay.format(formatter));
	logger.info(numberFour.format(formatter));

	assertTrue(numberFour.isBefore(newYearsDay));
}
 
Example 6
Source File: ExecutionQueryTest.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Test
@Deployment(resources = { "org/flowable/engine/test/api/oneTaskProcess.bpmn20.xml" })
public void testQueryLocalDateTimeVariable() throws Exception {
    Map<String, Object> vars = new HashMap<>();
    LocalDateTime localDateTime = LocalDateTime.now();
    vars.put("localDateTimeVar", localDateTime);

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

    LocalDateTime localDateTime2 = localDateTime.plusDays(1);
    vars = new HashMap<>();
    vars.put("localDateTimeVar", localDateTime);
    vars.put("localDateTimeVar2", localDateTime2);
    ProcessInstance processInstance2 = runtimeService.startProcessInstanceByKey("oneTaskProcess", vars);

    LocalDateTime nextYear = localDateTime.plusYears(1);
    vars = new HashMap<>();
    vars.put("localDateTimeVar", nextYear);
    ProcessInstance processInstance3 = runtimeService.startProcessInstanceByKey("oneTaskProcess", vars);

    LocalDateTime nextMonth = localDateTime.plusMonths(1);

    LocalDateTime twoYearsLater = localDateTime.plusYears(2);

    LocalDateTime oneYearAgo = localDateTime.minusYears(1);

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

    // Query on two localDateTime variables, should result in single value
    query = runtimeService.createExecutionQuery().variableValueEquals("localDateTimeVar", localDateTime).variableValueEquals("localDateTimeVar2", localDateTime2);
    Execution execution = query.singleResult();
    assertThat(execution).isNotNull();
    assertThat(execution.getId()).isEqualTo(processInstance2.getId());

    // Query with unexisting variable value
    execution = runtimeService.createExecutionQuery().variableValueEquals("localDateTimeVar", localDateTime.minusDays(1)).singleResult();
    assertThat(execution).isNull();

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

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

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

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

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

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

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

    assertThat(runtimeService.createExecutionQuery().variableValueLessThan("localDateTimeVar", localDateTime).count()).isZero();
    assertThat(runtimeService.createExecutionQuery().variableValueLessThan("localDateTimeVar", twoYearsLater).count()).isEqualTo(3);

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

    assertThat(runtimeService.createExecutionQuery().variableValueLessThanOrEqual("localDateTimeVar", 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(localDateTime).list();
    assertThat(executions)
        .extracting(Execution::getId)
        .containsExactlyInAnyOrder(
            processInstance1.getId(),
            processInstance2.getId()
        );

    execution = runtimeService.createExecutionQuery().variableValueEquals(twoYearsLater).singleResult();
    assertThat(execution).isNull();
}