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

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

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

LocalDateTime.minusMonths介绍

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

This method subtracts the specified amount from the months field in three steps:

  1. Subtract the input months from 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 minus 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: SonarSource/sonarqube

@Test
public void delete_ce_scanner_context_older_than_28_days() {
 LocalDateTime now = LocalDateTime.now();
 insertWithDate("VERY_OLD", now.minusDays(28).minusMonths(12));
 insertWithDate("JUST_OLD_ENOUGH", now.minusDays(28).minusDays(1));
 insertWithDate("NOT_OLD_ENOUGH", now.minusDays(28));
 insertWithDate("RECENT", now.minusDays(1));
 when(system2.now()).thenReturn(now.toInstant(ZoneOffset.UTC).toEpochMilli());
 underTest.start();
 assertThat(scannerContextExists("VERY_OLD")).isFalse();
 assertThat(scannerContextExists("JUST_OLD_ENOUGH")).isFalse();
 assertThat(scannerContextExists("NOT_OLD_ENOUGH")).isTrue();
 assertThat(scannerContextExists("RECENT")).isTrue();
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void delete_activity_older_than_180_days_and_their_scanner_context() {
 LocalDateTime now = LocalDateTime.now();
 insertWithDate("VERY_OLD", now.minusDays(180).minusMonths(10));
 insertWithDate("JUST_OLD_ENOUGH", now.minusDays(180).minusDays(1));
 insertWithDate("NOT_OLD_ENOUGH", now.minusDays(180));
 insertWithDate("RECENT", now.minusDays(1));
 when(system2.now()).thenReturn(now.toInstant(ZoneOffset.UTC).toEpochMilli());
 underTest.start();
 assertThat(selectActivity("VERY_OLD").isPresent()).isFalse();
 assertThat(selectTaskInput("VERY_OLD").isPresent()).isFalse();
 assertThat(selectTaskCharecteristic("VERY_OLD")).hasSize(0);
 assertThat(scannerContextExists("VERY_OLD")).isFalse();
 assertThat(selectActivity("JUST_OLD_ENOUGH").isPresent()).isFalse();
 assertThat(selectTaskInput("JUST_OLD_ENOUGH").isPresent()).isFalse();
 assertThat(selectTaskCharecteristic("JUST_OLD_ENOUGH")).hasSize(0);
 assertThat(scannerContextExists("JUST_OLD_ENOUGH")).isFalse();
 assertThat(selectActivity("NOT_OLD_ENOUGH").isPresent()).isTrue();
 assertThat(selectTaskInput("NOT_OLD_ENOUGH").isPresent()).isTrue();
 assertThat(selectTaskCharecteristic("NOT_OLD_ENOUGH")).hasSize(1);
 assertThat(scannerContextExists("NOT_OLD_ENOUGH")).isFalse(); // because more than 4 weeks old
 assertThat(selectActivity("RECENT").isPresent()).isTrue();
 assertThat(selectTaskInput("RECENT").isPresent()).isTrue();
 assertThat(selectTaskCharecteristic("RECENT")).hasSize(1);
 assertThat(scannerContextExists("RECENT")).isTrue();
}

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

LocalDateTime d = LocalDateTime.of(2016, Month.JANUARY, 28, 0, 0, 0);
System.out.println(d);
System.out.println(d.minusMonths(1));

代码示例来源:origin: br.com.jarch/jarch-annotation

@Override
public String getValueCloneTest() {
  return LocalDateTime.now().minusMonths(1).format(DateTimeFormatter.ofPattern(DDMMYYYY_HHMMSS));
}

代码示例来源:origin: br.com.jarch/jarch-annotation

@Override
public String getValueChangeTest() {
  return LocalDateTime.now().minusMonths(2).format(DateTimeFormatter.ofPattern(DDMMYYYY_HHMMSS));
}

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

LocalDateTime now = LocalDateTime.now();
LocalDateTime before = now.minusMonths(1).minusDays(5).minusHours(2).minusMinutes(30);
Period p = Period.between(before.toLocalDate(), now.toLocalDate());
Duration d = Duration.between(before.toLocalTime(), now.toLocalTime());

System.out.println(p + " + " + d);

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

diffPeriod = diffPeriod.withDays(0);
long monthDiff = diffPeriod.toTotalMonths() % period.toTotalMonths();
return start.plus(diffPeriod).minusMonths(monthDiff);

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

@Test
@Ignore
public void test() {
  doInJPA(entityManager -> {
    Event firstPartRelease = new Event();
    firstPartRelease.setName("High-Performance Java Persistence Part I");
    firstPartRelease.setCreatedOn(Timestamp.from(LocalDateTime.of(2015, 11, 2, 9, 0, 0).toInstant(ZoneOffset.UTC)));
    entityManager.persist(firstPartRelease);
    Event finalRelease = new Event();
    finalRelease.setName("High-Performance Java Persistence Part I");
    finalRelease.setCreatedOn(Timestamp.from(LocalDateTime.of(2016, 8, 25, 9, 0, 0).toInstant(ZoneOffset.UTC)));
    entityManager.persist(finalRelease);
  });
  doInJPA(entityManager -> {
    List<Event> events = entityManager.createQuery(
      "select e " +
      "from Event e " +
      "where e.createdOn > :timestamp", Event.class)
    .setParameter("timestamp", Timestamp.valueOf(LocalDateTime.now().minusMonths(1)))
    .getResultList();
  });
  doInJPA(entityManager -> {
    List<Event> events = entityManager.createNamedQuery("past_30_days_events").getResultList();
  });
}

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

@Test
public void test() {
  doInJPA(entityManager -> {
    User user = new User();
    user.setId(1L);
    user.setFirstName("John");
    user.setFirstName("Doe");
    entityManager.persist(user);
    Account account = new Account(
        1L,
        user,
        "ABC123",
        12345L,
        6.7,
        Timestamp.valueOf(
            LocalDateTime.now().minusMonths(3)
        )
    );
    entityManager.persist(account);
  });
  doInJPA(entityManager -> {
    Account account = entityManager.find(Account.class, 1L);
    assertEquals(123.45D, account.getDollars(), 0.001);
    assertEquals(207L, account.getInterestCents());
    assertEquals(2.07D, account.getInterestDollars(), 0.001);
  });
}

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

@Test
public void test() {
  doInJPA(entityManager -> {
    User user = new User();
    user.setId(1L);
    user.setFirstName("John");
    user.setFirstName("Doe");
    entityManager.persist(user);
    Account account = new Account(
      1L,
      user,
      "ABC123",
      12345L,
      6.7,
      Timestamp.valueOf(
          LocalDateTime.now().minusMonths(3)
      )
    );
    entityManager.persist(account);
  });
  doInJPA(entityManager -> {
    Account account = entityManager.find(Account.class, 1L);
    assertEquals(123.45D, account.getDollars(), 0.001);
    assertEquals(207L, account.getInterestCents());
    assertEquals(2.07D, account.getInterestDollars(), 0.001);
  });
}

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

@Test
public void test() {
  doInJPA(entityManager -> {
    User user = new User();
    user.setId(1L);
    user.setFirstName("John");
    user.setFirstName("Doe");
    entityManager.persist(user);
    Account account = new Account(
        1L,
        user,
        "ABC123",
        12345L,
        6.7,
        Timestamp.valueOf(
            LocalDateTime.now().minusMonths(3)
        )
    );
    entityManager.persist(account);
  });
  doInJPA(entityManager -> {
    Account account = entityManager.find(Account.class, 1L);
    assertEquals(123.45D, account.getDollars(), 0.001);
    assertEquals(207L, account.getInterestCents());
    assertEquals(2.07D, account.getInterestDollars(), 0.001);
  });
}

相关文章

LocalDateTime类方法