java.time.LocalDateTime.plusMonths()方法的使用及代码示例

x33g5p2x  于2022-01-23 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(421)

本文整理了Java中java.time.LocalDateTime.plusMonths()方法的一些代码示例,展示了LocalDateTime.plusMonths()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。LocalDateTime.plusMonths()方法的具体详情如下:
包路径:java.time.LocalDateTime
类名称:LocalDateTime
方法名:plusMonths

LocalDateTime.plusMonths介绍

[英]Returns a copy of this LocalDateTime with the specified period in months added.

This method adds the specified amount to the months field in three steps:

  1. Add the input months to the month-of-year field
  2. Check if the resulting date would be invalid
  3. Adjust the day-of-month to the last valid day if necessary

For example, 2007-03-31 plus one month would result in the invalid date 2007-04-31. Instead of returning an invalid result, the last valid day of the month, 2007-04-30, is selected instead.

This instance is immutable and unaffected by this method call.
[中]返回此LocalDateTime的副本,并添加指定的期间(以月为单位)。
此方法分三步将指定金额添加到“月份”字段:
1.将输入月份添加到“年度月份”字段
1.检查结果日期是否无效
1.如有必要,将月份日期调整为最后一个有效日期
例如,2007-03-31加上一个月将导致无效日期2007-04-31。将选择月份的最后一个有效日期2007-04-30,而不是返回无效结果。
此实例是不可变的,不受此方法调用的影响。

代码示例

代码示例来源:origin: yu199195/Raincat

/**
 * 计算 month 月后的时间.
 *
 * @param date  长日期
 * @param month 需要增加的月数
 * @return 增加后的日期
 */
public static LocalDateTime addMoth(final LocalDateTime date, final int month) {
  return date.plusMonths(month);
}

代码示例来源:origin: yu199195/Raincat

/**
 * 获得当天近一月.
 *
 * @return 日期
 */
public static LocalDateTime getAMonthFromNow() {
  LocalDateTime date = LocalDateTime.now();
  //一个月前
  return date.plusMonths(-1);
}

代码示例来源:origin: stackoverflow.com

tempDateTime = tempDateTime.plusMonths( months );

代码示例来源:origin: SeanDragon/protools

public DatePlus addMonth(long month) {
  this.localDateTime = this.localDateTime.plusMonths(month);
  return this;
}

代码示例来源:origin: lets-blade/blade

@Test
public void testPrettyTime() {
  Assert.assertEquals("去年", DateKit.prettyTime(LocalDateTime.now().plusYears(-1), Locale.CHINESE));
  Assert.assertEquals("上个月", DateKit.prettyTime(LocalDateTime.now().plusMonths(-1), Locale.CHINESE));
  Assert.assertEquals("上周", DateKit.prettyTime(LocalDateTime.now().plusWeeks(-1), Locale.CHINESE));
  Assert.assertEquals("昨天", DateKit.prettyTime(LocalDateTime.now().plusDays(-1), Locale.CHINESE));
  Assert.assertEquals("1小时前", DateKit.prettyTime(LocalDateTime.now().plusHours(-1), Locale.CHINESE));
  Assert.assertEquals("1分钟前", DateKit.prettyTime(LocalDateTime.now().plusMinutes(-1), Locale.CHINESE));
  Assert.assertEquals("刚刚", DateKit.prettyTime(LocalDateTime.now().plusSeconds(-1), Locale.CHINESE));
  Assert.assertEquals("10秒前", DateKit.prettyTime(LocalDateTime.now().plusSeconds(-10), Locale.CHINESE));
}

代码示例来源:origin: jtablesaw/tablesaw

@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);
}

代码示例来源:origin: keets2012/Lottor

/**
 * 计算 month 月后的时间
 *
 * @param date  长日期
 * @param month 需要增加的月数
 * @return 增加后的日期
 */
public static LocalDateTime addMoth(LocalDateTime date, int month) {
  return date.plusMonths(month);
}

代码示例来源:origin: com.liumapp.qtools.date/qtools-date

public DatePlus addMonth(long month) {
  this.localDateTime = this.localDateTime.plusMonths(month);
  return this;
}

代码示例来源:origin: keets2012/Lottor

/**
 * 获得当天近一月
 * @return LocalDateTime
 */
public static LocalDateTime getAMonthFromNow() {
  LocalDateTime date = LocalDateTime.now();
  //一个月前
  return date.plusMonths(-1);
}

代码示例来源:origin: youtongluan/sumk

/**
 * months个月以后的日期
 * 
 * @param months
 *            任意数字,正数、负数都可以
 * @return months个月以后的日期
 */
public SumkDate plusMonths(int months) {
  if (months == 0) {
    return this;
  }
  return of(this.toLocalDateTime().plusMonths(months));
}

代码示例来源:origin: de.alpharogroup/user-management-business

/**
 * New user tokens.
 *
 * @param username
 *            the username
 * @return the user tokens
 */
private UserTokens newUserTokens(final String username)
{
  UserTokens userTokens;
  // expires in one year
  final Date expiry = Date
    .from(LocalDateTime.now().plusMonths(12).atZone(ZoneId.systemDefault()).toInstant());
  // create a token
  final String token = RandomExtensions.randomToken();
  userTokens = UserTokens.builder().expiry(expiry).username(username).token(token).build();
  return userTokens;
}

代码示例来源:origin: com.intrbiz.bergamot/bergamot-timerange

@Override
public LocalDateTime computeNextStartTime(Clock clock)
{
  LocalDateTime next = super.computeNextStartTime(clock);
  if (next == null) return null;
  // set the day of month
  int dom = this.dayOfMonth > 0 ? this.dayOfMonth : (next.getMonth().maxLength() + this.dayOfMonth + 1);
  next = next.withDayOfMonth(dom);
  // is it next month
  if (! next.isAfter(LocalDateTime.now(clock))) next = next.plusMonths(1);
  // check the date is valid
  return (next.isAfter(LocalDateTime.now(clock))) ? next : null;
}

代码示例来源:origin: xautlx/s2jh4net

@RequestMapping(value = "/account/profile/password", method = RequestMethod.POST)
  @ResponseBody
  public OperationResult modifyPasswordSave(@AuthAccount Account account, @RequestParam("oldpasswd") String oldpasswd,
                       @RequestParam("newpasswd") String newpasswd) {
    Validation.notDemoMode();
    String encodedPasswd = accountService.encodeUserPasswd(account, oldpasswd);
    if (!encodedPasswd.equals(account.getPassword())) {
      return OperationResult.buildFailureResult("原密码不正确,请重新输入");
    } else {
      //更新密码失效日期为6个月后
      account.setCredentialsExpireDate(DateUtils.currentDateTime().plusMonths(6).toLocalDate());
      accountService.save(account, newpasswd);
      return OperationResult.buildSuccessResult("密码修改成功,请在下次登录使用新密码");
    }
  }
}

代码示例来源:origin: xautlx/s2jh4net

@RequestMapping(value = "/user/password", method = RequestMethod.POST)
@ResponseBody
public OperationResult modifyPasswordSave(@ApiIgnore @AuthAccount Account account,
                     @RequestParam("oldpasswd") String oldpasswd,
                     @RequestParam("newpasswd") String newpasswd) {
  Validation.notDemoMode();
  String encodedPasswd = accountService.encodeUserPasswd(account, oldpasswd);
  if (!encodedPasswd.equals(account.getPassword())) {
    return OperationResult.buildFailureResult("原密码不正确,请重新输入");
  } else {
    //更新密码失效日期为6个月后
    account.setCredentialsExpireDate(DateUtils.currentDateTime().plusMonths(6).toLocalDate());
    accountService.save(account, newpasswd);
    return OperationResult.buildSuccessResult("密码修改成功");
  }
}

代码示例来源:origin: RoboZonky/robozonky

@Test
  void setExitStrategy() {
    setClock(Clock.fixed(Instant.EPOCH, Defaults.ZONE_ID));
    final ExitProperties p = new ExitProperties(DateUtil.localNow().plusMonths(1).toLocalDate());
    final DefaultValues v = new DefaultValues(DefaultPortfolio.EMPTY);
    assertThat(v.getMonthsBeforeExit()).isEqualTo(-1);
    v.setExitProperties(p);
    assertThat(v.getMonthsBeforeExit()).isEqualTo(1);
  }
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Override
public void afterInit() {
  doInJPA(entityManager -> {
    Post post = new Post();
    post.setOwner("Vlad");
    post.setTitle("Inheritance");
    post.setContent("Best practices");
    entityManager.persist(post);
    Announcement announcement = new Announcement();
    announcement.setOwner("Vlad");
    announcement.setTitle("Release x.y.z.Final");
    announcement.setValidUntil(Timestamp.valueOf(LocalDateTime.now().plusMonths(1)));
    entityManager.persist(announcement);
  });
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Override
public void afterInit() {
  doInJPA(entityManager -> {
    Post post = new Post();
    post.setOwner("Vlad");
    post.setTitle("Inheritance");
    post.setContent("Best practices");
    entityManager.persist(post);
    Announcement announcement = new Announcement();
    announcement.setOwner("Vlad");
    announcement.setTitle("Release x.y.z.Final");
    announcement.setValidUntil(Timestamp.valueOf(LocalDateTime.now().plusMonths(1)));
    entityManager.persist(announcement);
  });
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
public void test() {
  doInJPA(entityManager -> {
    Post post = new Post();
    post.setOwner("John Doe");
    post.setTitle("Inheritance");
    post.setContent("Best practices");
    entityManager.persist(post);
    Announcement announcement = new Announcement();
    announcement.setOwner("John Doe");
    announcement.setTitle("Release x.y.z.Final");
    announcement.setValidUntil(Timestamp.valueOf(LocalDateTime.now().plusMonths(1)));
    entityManager.persist(announcement);
  });
  doInJPA(entityManager -> {
    List<Post> posts = entityManager
    .createQuery(
      "select p " +
      "from Post p ", Post.class)
    .getResultList();
    assertEquals(1, posts.size());
  });
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
public void test() {
  doInJPA(entityManager -> {
    Post post = new Post();
    post.setOwner("John Doe");
    post.setTitle("Inheritance");
    post.setContent("Best practices");
    entityManager.persist(post);
    Announcement announcement = new Announcement();
    announcement.setOwner("John Doe");
    announcement.setTitle("Release x.y.z.Final");
    announcement.setValidUntil(Timestamp.valueOf(LocalDateTime.now().plusMonths(1)));
    entityManager.persist(announcement);
  });
  doInJPA(entityManager -> {
    List<Post> posts = entityManager
    .createQuery(
      "select p " +
      "from Post p ", Post.class)
    .getResultList();
    assertEquals(1, posts.size());
  });
}

代码示例来源:origin: ch.sahits.game/OpenPatricianDisplay

@Override
  public void run() {
    int penalty = (int)(Math.abs(player.getCompany().getCash()/(200*rnd.nextDouble())));
    DisplayMessage msg = new DisplayMessage("ch.sahits.game.graphic.event.task.DelayedCaughtAction.message", penalty);
    clientEventBus.post(msg);
    ILoaner loaner = loaners.getLoaner(city);
    IDebt debt = Debt.builder()
        .amount(penalty)
        .dueDate(date.getCurrentDate().plusMonths(1))
        .interest(0)
        .loanTakeOut(date.getCurrentDate())
        .debitor(player)
        .creditor(new CityCreditor())
        .build();
    loaner.addDebt(debt);
  }
}

相关文章

LocalDateTime类方法