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

The following examples show how to use java.time.LocalDateTime#plusYears() . 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
@Test
public void testAuditQueryBuilder()
{
    String uid = CodeGenerator.generateUid();
    String code = CodeGenerator.generateUid();

    LocalDateTime dateFrom = LocalDateTime.of( 2010, 4, 6, 12, 0, 0 );
    LocalDateTime dateTo = dateFrom.plusYears( 4 );

    // TODO should we add bean validation in AuditQuery so we know the from is before to
    assertTrue( dateFrom.isBefore( dateTo ) );

    AuditQuery query = AuditQuery.builder()
        .klass( Sets.newHashSet( DataElement.class.getName() ) )
        .uid( Sets.newHashSet( uid ) )
        .code( Sets.newHashSet( code ) )
        .range( AuditQuery.range( dateFrom, dateTo ) )
        .build();

    assertEquals( Sets.newHashSet( DataElement.class.getName() ), query.getKlass() );
    assertEquals( Sets.newHashSet( uid ), query.getUid() );
    assertEquals( Sets.newHashSet( code ), query.getCode() );
    assertEquals( dateFrom, query.getRange().getFrom() );
    assertEquals( dateTo, query.getRange().getTo() );
}
 
Example 2
/**
 * Get date list - String
 * @param stdate Start date
 * @param enddate End date
 * @param tdtype Calendar type
 * @param timeDelt Time delta value
 * @return Date list
 * @throws FileNotFoundException
 * @throws IOException
 * @throws ParseException 
 */
public static List<LocalDateTime> getDateList(LocalDateTime stdate, LocalDateTime enddate, String tdtype, int timeDelt)
        throws FileNotFoundException, IOException, ParseException {
    List<LocalDateTime> dates = new ArrayList<>();

    while (stdate.isBefore(enddate)) {
        dates.add(stdate);
        switch (tdtype.toUpperCase()) {
            case "YEAR":
                stdate = stdate.plusYears(timeDelt);
                break;
            case "MONTH":
                stdate = stdate.plusMonths(timeDelt);
                break;
            case "DAY":
                stdate = stdate.plusDays(timeDelt);
                break;                
            case "HOUR":
                stdate = stdate.plusHours(timeDelt);
                break;
            case "MINUTE":
                stdate = stdate.plusMinutes(timeDelt);
                break;
            default:
                stdate = stdate.plusSeconds(timeDelt);
                break;
        }            
    }
    dates.add(enddate);
    return dates;
}
 
Example 3
@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 4
@Test
@Deployment(resources = "org/flowable/engine/test/api/oneTaskProcess.bpmn20.xml")
void testGetLocalDateTimeVariableFromTask() {
    ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder()
            .processDefinitionKey("oneTaskProcess")
            .start();

    Map<String, Object> variables = new HashMap<>();
    LocalDateTime nowLocalDateTime = LocalDateTime.now().truncatedTo(ChronoUnit.MILLIS);
    LocalDateTime oneYearLater = nowLocalDateTime.plusYears(1);
    variables.put("nowLocalDateTime", nowLocalDateTime);
    variables.put("oneYearLater", oneYearLater);
    Task task = taskService.createTaskQuery().singleResult();
    taskService.setVariables(task.getId(), variables);

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

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

    assertThat(taskService.getVariables(task.getId()))
            .containsOnly(
                    entry("nowLocalDateTime", nowLocalDateTime),
                    entry("oneYearLater", oneYearLater)
            );
}
 
Example 5
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 6
public String createRelativeDateTime(LocalDateTime dateT) {
  LocalDateTime currentDateTime = LocalDateTime.now();

  long years = currentDateTime.until(dateT, ChronoUnit.YEARS);
  currentDateTime = currentDateTime.plusYears(years);

  long months = currentDateTime.until(dateT, ChronoUnit.MONTHS);
  currentDateTime = currentDateTime.plusMonths(months);

  long days = currentDateTime.until(dateT, ChronoUnit.DAYS);
  currentDateTime = currentDateTime.plusDays(days);

  long hours = currentDateTime.until(dateT, ChronoUnit.HOURS);
  currentDateTime = currentDateTime.plusHours(hours);

  long minutes = currentDateTime.until(dateT, ChronoUnit.MINUTES);
  currentDateTime = currentDateTime.plusMinutes(minutes);

  long seconds = currentDateTime.until(dateT, ChronoUnit.SECONDS);
  if (seconds < 0 || minutes < 0 || hours < 0 || days < 0 || months < 0 || years < 0) {
    return "NOW["
        + ((years == 0) ? "" : (years + "y"))
        + ((months == 0) ? "" : (months + "M"))
        + ((days == 0) ? "" : (days + "d"))
        + ((hours == 0) ? "" : (hours + "H"))
        + ((minutes == 0) ? "" : (minutes + "m"))
        + ((seconds == 0) ? "" : (seconds + "s"))
        + "]";
  }
  return "NOW["
      + ((years == 0) ? "" : ("+" + years + "y"))
      + ((months == 0) ? "" : ("+" + months + "M"))
      + ((days == 0) ? "" : ("+" + days + "d"))
      + ((hours == 0) ? "" : ("+" + hours + "H"))
      + ((minutes == 0) ? "" : ("+" + minutes + "m"))
      + ((seconds == 0) ? "" : ("+" + seconds + "s"))
      + "]";
}
 
Example 7
@Test
public void add_years_to_date_in_java8() {

	LocalDateTime superBowlXLV = LocalDateTime.of(2011, Month.FEBRUARY, 6,
			0, 0);
	LocalDateTime fortyNinersSuck = superBowlXLV.plusYears(2);

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

	logger.info(superBowlXLV.format(formatter));
	logger.info(fortyNinersSuck.format(formatter));

	assertTrue(fortyNinersSuck.isAfter(superBowlXLV));
}
 
Example 8
Source Project: Lottor   File: DateUtils.java    License: MIT License 4 votes vote down vote up
/**
 *  获得当天近一年
 * @return LocalDateTime
 */
public static LocalDateTime getAYearFromNow() {
    LocalDateTime date = LocalDateTime.now();
    //一年前
    return date.plusYears(-1);
}
 
Example 9
/**
 * 获得当天近一年.
 *
 * @return 日期 a year from now
 */
public static LocalDateTime getAYearFromNow() {
    LocalDateTime date = LocalDateTime.now();
    //一年前
    return date.plusYears(-1);
}
 
Example 10
@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();
}
 
Example 11
Source Project: Lottor   File: DateUtils.java    License: MIT License 2 votes vote down vote up
/**
 * 计算 year 年后的时间
 *
 * @param date 长日期
 * @param year 需要增加的年数
 * @return 增加后的日期
 */
public static LocalDateTime addYear(LocalDateTime date, int year) {
    return date.plusYears(year);
}
 
Example 12
/**
 * 计算 year 年后的时间.
 *
 * @param date 长日期
 * @param year 需要增加的年数
 * @return 增加后的日期 local date time
 */
public static LocalDateTime addYear(final LocalDateTime date, final int year) {
    return date.plusYears(year);
}