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

The following examples show how to use java.time.LocalDateTime#plusMonths() . 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: MonthlyInterestStrategy.java    From kid-bank with Apache License 2.0 6 votes vote down vote up
@Override
public void createInterestCreditTransactionsFor(InterestEarningAccount account) {
  Optional<LocalDateTime> firstTransactionDateTime = firstTransactionDateTime(account);

  if (!firstTransactionDateTime.isPresent()) {
    return;
  }

  LocalDateTime creditDateTime = firstDayOfMonthAfter(firstTransactionDateTime.get());

  LocalDateTime lastDateTimeForCredit = firstDayOfNowMonth();

  // loop through first of current clock's month
  while (!creditDateTime.isAfter(lastDateTimeForCredit)) {
    int interestToCredit = calculateInterest(account.balanceUpTo(creditDateTime));
    account.interestCredit(creditDateTime, interestToCredit);
    creditDateTime = creditDateTime.plusMonths(1);
  }
}
 
Example 2
Source File: TimeTableData.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 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
Source File: DateTimeFiltersTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetMonthValue() {
  LocalDateTime date = LocalDate.of(2015, 1, 25).atStartOfDay();
  Month[] months = Month.values();

  DateTimeColumn dateTimeColumn = DateTimeColumn.create("test");
  for (int i = 0; i < months.length; i++) {
    dateTimeColumn.append(date);
    date = date.plusMonths(1);
  }

  assertTrue(dateTimeColumn.isInJanuary().contains(0));
  assertTrue(dateTimeColumn.isInFebruary().contains(1));
  assertTrue(dateTimeColumn.isInMarch().contains(2));
  assertTrue(dateTimeColumn.isInApril().contains(3));
  assertTrue(dateTimeColumn.isInMay().contains(4));
  assertTrue(dateTimeColumn.isInJune().contains(5));
  assertTrue(dateTimeColumn.isInJuly().contains(6));
  assertTrue(dateTimeColumn.isInAugust().contains(7));
  assertTrue(dateTimeColumn.isInSeptember().contains(8));
  assertTrue(dateTimeColumn.isInOctober().contains(9));
  assertTrue(dateTimeColumn.isInNovember().contains(10));
  assertTrue(dateTimeColumn.isInDecember().contains(11));

  assertTrue(dateTimeColumn.isInQ1().contains(2));
  assertTrue(dateTimeColumn.isInQ2().contains(4));
  assertTrue(dateTimeColumn.isInQ3().contains(8));
  assertTrue(dateTimeColumn.isInQ4().contains(11));

  Table t = Table.create("Test");
  t.addColumns(dateTimeColumn);
  IntColumn index = IntColumn.indexColumn("index", t.rowCount(), 0);
  t.addColumns(index);
}
 
Example 4
Source File: DateTimeFiltersTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetMonthValue() {
  LocalDateTime date = LocalDate.of(2015, 1, 25).atStartOfDay();
  Month[] months = Month.values();

  DateTimeColumn dateTimeColumn = DateTimeColumn.create("test");
  for (int i = 0; i < months.length; i++) {
    dateTimeColumn.append(date);
    date = date.plusMonths(1);
  }

  assertTrue(dateTimeColumn.isInJanuary().contains(0));
  assertTrue(dateTimeColumn.isInFebruary().contains(1));
  assertTrue(dateTimeColumn.isInMarch().contains(2));
  assertTrue(dateTimeColumn.isInApril().contains(3));
  assertTrue(dateTimeColumn.isInMay().contains(4));
  assertTrue(dateTimeColumn.isInJune().contains(5));
  assertTrue(dateTimeColumn.isInJuly().contains(6));
  assertTrue(dateTimeColumn.isInAugust().contains(7));
  assertTrue(dateTimeColumn.isInSeptember().contains(8));
  assertTrue(dateTimeColumn.isInOctober().contains(9));
  assertTrue(dateTimeColumn.isInNovember().contains(10));
  assertTrue(dateTimeColumn.isInDecember().contains(11));

  assertTrue(dateTimeColumn.isInQ1().contains(2));
  assertTrue(dateTimeColumn.isInQ2().contains(4));
  assertTrue(dateTimeColumn.isInQ3().contains(8));
  assertTrue(dateTimeColumn.isInQ4().contains(11));

  Table t = Table.create("Test");
  t.addColumns(dateTimeColumn);
  IntColumn index = IntColumn.indexColumn("index", t.rowCount(), 0);
  t.addColumns(index);
}
 
Example 5
Source File: ImportDateTime.java    From axelor-open-suite with GNU Affero General Public License v3.0 5 votes vote down vote up
public LocalDateTime updateMonth(LocalDateTime datetime, String month) {
  if (!Strings.isNullOrEmpty(month)) {
    Matcher matcher = patternMonth.matcher(month);
    if (matcher.find()) {
      Long months = Long.parseLong(matcher.group());
      if (month.startsWith("+")) datetime = datetime.plusMonths(months);
      else if (month.startsWith("-")) datetime = datetime.minusMonths(months);
      else datetime = datetime.withMonth(months.intValue());
    }
  }
  return datetime;
}
 
Example 6
Source File: DataBackupCreateService.java    From axelor-open-suite with GNU Affero General Public License v3.0 5 votes vote down vote up
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
Source File: DatePlusMonths.java    From levelup-java-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void add_months_to_date_in_java8() {

	LocalDateTime superBowlXLV = LocalDateTime.of(2011, Month.FEBRUARY, 6,
			0, 0);
	LocalDateTime sippinFruityDrinksInMexico = superBowlXLV.plusMonths(1);

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

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

	assertTrue(sippinFruityDrinksInMexico.isAfter(superBowlXLV));
}
 
Example 8
Source File: TimeAxis.java    From MeteoInfo with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Update time tick values
 */
private void updateTimeTickValues() {
    LocalDateTime sdate = JDateUtil.fromOADate(this.getMinValue());
    LocalDateTime edate = JDateUtil.fromOADate(this.getMaxValue());
    LocalDateTime ssdate = LocalDateTime.of(sdate.getYear(), sdate.getMonthValue(), sdate.getDayOfMonth(),
            sdate.getHour(), sdate.getMinute(), sdate.getSecond());

    List<LocalDateTime> dates = new ArrayList<>();
    switch (this.timeUnit) {
        case YEAR:
            sdate = LocalDateTime.of(sdate.getYear(), 1, 1, 0, 0, 0);
            if (!sdate.isBefore(ssdate)) {
                dates.add(sdate);
            }
            while (!sdate.isAfter(edate)) {
                sdate = sdate.withYear(sdate.getYear() + 1);
                dates.add(sdate);
            }
            break;
        case MONTH:
            sdate = LocalDateTime.of(sdate.getYear(), sdate.getMonthValue(), 1, 0, 0, 0);
            if (!sdate.isBefore(ssdate)) {
                dates.add(sdate);
            }
            while (!sdate.isAfter(edate)) {
                sdate = sdate.plusMonths(1);
                if (!sdate.isBefore(ssdate)) {
                    dates.add(sdate);
                }
            }
            break;
        case DAY:
            sdate = LocalDateTime.of(sdate.getYear(), sdate.getMonthValue(), sdate.getDayOfMonth(),
                    0, 0, 0);
            if (!sdate.isBefore(ssdate)) {
                dates.add(sdate);
            }
            while (!sdate.isAfter(edate)) {
                sdate = sdate.plusDays(1);
                if (sdate.isBefore(edate)) {
                    dates.add(sdate);
                }
            }
            break;
        case HOUR:
            sdate = LocalDateTime.of(sdate.getYear(), sdate.getMonthValue(), sdate.getDayOfMonth(),
                    sdate.getHour(), 0, 0);
            if (!sdate.isBefore(ssdate)) {
                dates.add(sdate);
            }
            while (!sdate.isAfter(edate)) {
                sdate = sdate.plusHours(1);
                if (sdate.isBefore(edate)) {
                    dates.add(sdate);
                }
            }
            break;
        case MINUTE:
            sdate = ssdate.withSecond(0);
            if (!sdate.isBefore(ssdate)) {
                dates.add(sdate);
            }
            while (!sdate.isAfter(edate)) {
                sdate = sdate.plusMinutes(1);
                if (sdate.isBefore(edate)) {
                    dates.add(sdate);
                }
            }
            break;
        case SECOND:
            if (!sdate.isBefore(ssdate)) {
                dates.add(sdate);
            }
            while (!sdate.isAfter(edate)) {
                sdate = sdate.plusSeconds(1);
                if (sdate.isBefore(edate)) {
                    dates.add(sdate);
                }
            }
            break;
    }

    double[] tvs = new double[dates.size()];
    for (int i = 0; i < dates.size(); i++) {
        tvs[i] = JDateUtil.toOADate(dates.get(i));
    }
    this.setTickValues(tvs);
}
 
Example 9
Source File: DateUtils.java    From Lottor with MIT License 4 votes vote down vote up
/**
 * 获得当天近一月
 * @return LocalDateTime
 */
public static LocalDateTime getAMonthFromNow() {
    LocalDateTime date = LocalDateTime.now();
    //一个月前
    return date.plusMonths(-1);
}
 
Example 10
Source File: DateUtils.java    From Raincat with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 获得当天近一月.
 *
 * @return 日期 a month from now
 */
public static LocalDateTime getAMonthFromNow() {
    LocalDateTime date = LocalDateTime.now();
    //一个月前
    return date.plusMonths(-1);
}
 
Example 11
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();
}
 
Example 12
Source File: DateUtils.java    From Lottor with MIT License 2 votes vote down vote up
/**
 * 计算 month 月后的时间
 *
 * @param date  长日期
 * @param month 需要增加的月数
 * @return 增加后的日期
 */
public static LocalDateTime addMoth(LocalDateTime date, int month) {
    return date.plusMonths(month);
}
 
Example 13
Source File: DateUtils.java    From Raincat with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * 计算 month 月后的时间.
 *
 * @param date  长日期
 * @param month 需要增加的月数
 * @return 增加后的日期 local date time
 */
public static LocalDateTime addMoth(final LocalDateTime date, final int month) {
    return date.plusMonths(month);
}