java.time.OffsetDateTime.plusYears()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(3.4k)|赞(0)|评价(0)|浏览(156)

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

OffsetDateTime.plusYears介绍

[英]Returns a copy of this OffsetDateTime with the specified period in years added.

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

  1. Add the input years to 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) plus 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.
[中]返回此OffsetDateTime的副本,并添加指定的期间(以年为单位)。
此方法分三步将指定金额添加到“年份”字段:
1.将输入年份添加到年份字段
1.检查结果日期是否无效
1.如有必要,将月份日期调整为最后一个有效日期
例如,2008-02-29(闰年)加上一年将导致无效日期2009-02-29(标准年)。而不是返回无效结果,而是选择当月的最后一个有效日期2009-02-28。
此实例是不可变的,不受此方法调用的影响。

代码示例

代码示例来源:origin: google/bundletool

Instant notAfter = notBefore.atOffset(ZoneOffset.UTC).plusYears(30).toInstant();

代码示例来源:origin: com.github.seratch/java-time-backport

/**
 * Returns a copy of this {@code OffsetDateTime} with the specified period in years subtracted.
 * <p>
 * This method subtracts the specified amount from the years field in three steps:
 * <ol>
 * <li>Subtract the input years to the year field</li>
 * <li>Check if the resulting date would be invalid</li>
 * <li>Adjust the day-of-month to the last valid day if necessary</li>
 * </ol>
 * <p>
 * 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.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param years  the years to subtract, may be negative
 * @return an {@code OffsetDateTime} based on this date-time with the years subtracted, not null
 * @throws DateTimeException if the result exceeds the supported date range
 */
public OffsetDateTime minusYears(long years) {
  return (years == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-years));
}

代码示例来源:origin: org.openbase.bco/ontology.lib

/**
   * Method adds/subtracts time from the current dateTime.
   *
   * @param minutes are the minutes.
   * @param hours are the hours.
   * @param days are the days.
   * @param months are the months.
   * @param years are the years.
   * @return the changed dateTime as String.
   */
  public static String addTimeToCurrentDateTime(final int minutes, final int hours, final int days, final int months, final int years) {
    final OffsetDateTime now = OffsetDateTime.now();

    now.plusMinutes(minutes);
    now.plusHours(hours);
    now.plusDays(days);
    now.plusMonths(months);
    now.plusYears(years);

    return now.toString();
  }
}

代码示例来源:origin: com.sqlapp/sqlapp-core

/**
 * 年の加算を実行します
 * 
 * @param date
 *            日付型
 * @param years
 *            加算する年
 * @return 年を加算した結果のカレンダー
 */
@SuppressWarnings("unchecked")
public static <T extends Temporal> T addYears(final T date, final int years) {
  if (date == null) {
    return null;
  }
  if (date instanceof LocalDate){
    return (T)((LocalDate)date).plusYears(years);
  }else if (date instanceof LocalDateTime){
    return (T)((LocalDateTime)date).plusYears(years);
  }else if (date instanceof OffsetDateTime){
    return (T)((OffsetDateTime)date).plusYears(years);
  }else if (date instanceof ZonedDateTime){
    return (T)((ZonedDateTime)date).plusYears(years);
  }else if (date instanceof YearMonth){
    return (T)((YearMonth)date).plusYears(years);
  }
  return (T)date.plus(Duration.of(years, ChronoUnit.YEARS));
}

相关文章