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

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

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

LocalDateTime.minusYears介绍

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

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

  1. Subtract the input years from the 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, 2008-02-29 (leap year) minus one year would result in the invalid date 2009-02-29 (standard year). Instead of returning an invalid result, the last valid day of the month, 2009-02-28, is selected instead.

This instance is immutable and unaffected by this method call.
[中]返回此LocalDateTime的副本,并减去指定的期间(以年为单位)。
此方法分三步从年份字段中减去指定金额:
1.从年份字段中减去输入年份
1.检查结果日期是否无效
1.如有必要,将月份日期调整为最后一个有效日期
例如,2008-02-29(闰年)减去一年将导致无效日期2009-02-29(标准年)。将选择当月的最后一个有效日期2009-02-28,而不是返回无效结果。
此实例是不可变的,不受此方法调用的影响。

代码示例

代码示例来源:origin: biezhi/learn-java8

public static void main(String[] args) {

    // 创建一个LocalDateTime实例
    LocalDateTime localDateTime = LocalDateTime.now();

    // 使用指定的年月日、时分秒、纳秒来新建对象
    LocalDateTime localDateTime2 = LocalDateTime.of(2018, 11, 26, 13, 55, 36, 123);

    // 3年后的现在
    LocalDateTime dt1 = localDateTime.plusYears(3);
    // 3年前的现在
    LocalDateTime dt2 = localDateTime.minusYears(3);

    System.out.println("localDateTime  : " + localDateTime);
    System.out.println("localDateTime2 : " + localDateTime2);
    System.out.println("dt1            : " + dt1);
    System.out.println("dt2            : " + dt2);
  }
}

代码示例来源:origin: qala-io/datagen

public static LocalDateTime yearAgo() {
  return now().minusYears(1);
}
public static LocalDateTime yearsAgo(int n) {

代码示例来源:origin: qala-io/datagen

public static LocalDateTime yearsAgo(int n) {
  return now().minusYears(n);
}

代码示例来源:origin: drallieiv/KinanCity

public static String randomDateOfBirth(int minAge, int maxAge) {
  int randomDays = RandomUtils.nextInt((maxAge-minAge) * 365);
  LocalDateTime dobDate = LocalDateTime.now().minusYears(minAge).minusDays(randomDays);
  return dobDate.format(DateTimeFormatter.ISO_DATE);
}

代码示例来源:origin: cheese10yun/blog-sample

@Bean
@StepScope
public ListItemReader<User> inactiveUserReader() {
  List<User> oldUsers =
      userRepository.findByUpdatedDateBeforeAndStatusEquals(
          LocalDateTime.now().minusYears(1),
          UserStatus.ACTIVE);
  return new ListItemReader<>(oldUsers);
}

代码示例来源:origin: Devskiller/jfairy

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

代码示例来源:origin: de.knightsoft-net/mt-bean-validators

/**
 * get correct test beans.
 *
 * @return correct test beans
 */
public static final List<AgeLimitTestBeanLocalDateTime> getCorrectTestBeans() {
 final List<AgeLimitTestBeanLocalDateTime> correctCases = new ArrayList<>();
 correctCases.add(new AgeLimitTestBeanLocalDateTime(
   LocalDateTime.now().minusYears(AgeLimitTestBeanLocalDate.AGE_LIMIT + 1)));
 correctCases.add(new AgeLimitTestBeanLocalDateTime(
   LocalDateTime.now().minusYears(AgeLimitTestBeanLocalDate.AGE_LIMIT)));
 return correctCases;
}

代码示例来源:origin: de.knightsoft-net/mt-bean-validators

/**
  * get wrong test beans.
  *
  * @return wrong test beans
  */
 public static final List<AgeLimitTestBeanLocalDateTime> getWrongTestBeans() {
  final List<AgeLimitTestBeanLocalDateTime> wrongCases = new ArrayList<>();
  wrongCases.add(new AgeLimitTestBeanLocalDateTime(LocalDateTime.now()));
  wrongCases.add(new AgeLimitTestBeanLocalDateTime(
    LocalDateTime.now().minusYears(AgeLimitTestBeanLocalDate.AGE_LIMIT).plusDays(1)));
  return wrongCases;
 }
}

代码示例来源:origin: de.knightsoft-net/mt-bean-validators

@Override
 public final boolean isValid(final LocalDateTime pvalue,
   final ConstraintValidatorContext pcontext) {
  if (pvalue == null) {
   return true;
  }
  final LocalDateTime dateLimit = LocalDateTime.now().minusYears(minYears).withHour(0)
    .withMinute(0).withSecond(0).withNano(0);
  return !dateLimit.isBefore(pvalue.withHour(0).withMinute(0).withSecond(0).withNano(0));
 }
}

代码示例来源:origin: com.healthmarketscience.jackcess/jackcess

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

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

diffPeriod.withMonths(0);
long yearsDiff = diffPeriod.getYears() % period.getYears();
return start.plus(diffPeriod).minusYears(yearsDiff);

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

.minusYears(rnd.nextInt(15) + 15)
    .minusDays(rnd.nextInt(365)); // 15 - 30 year before
int conHer = 0;

相关文章

LocalDateTime类方法