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

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

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

OffsetDateTime.plusMonths介绍

[英]Returns a copy of this OffsetDateTime 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.
[中]返回此OffsetDateTime的副本,并添加指定的期间(以月为单位)。
此方法分三步将指定金额添加到“月份”字段:
1.将输入月份添加到“年度月份”字段
1.检查结果日期是否无效
1.如有必要,将月份日期调整为最后一个有效日期
例如,2007-03-31加上一个月将导致无效日期2007-04-31。而不是返回无效结果,而是选择当月的最后一个有效日期2007-04-30。
此实例是不可变的,不受此方法调用的影响。

代码示例

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

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

代码示例来源: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 months
 *            加算する月
 * @return 月を加算した結果のカレンダー
 */
@SuppressWarnings("unchecked")
public static <T extends Temporal> T addMonths(final T date, final int months) {
  if (date == null) {
    return null;
  }
  if (date instanceof LocalDate){
    return (T)((LocalDate)date).plusMonths(months);
  }else if (date instanceof LocalDateTime){
    return (T)((LocalDateTime)date).plusMonths(months);
  }else if (date instanceof OffsetDateTime){
    return (T)((OffsetDateTime)date).plusMonths(months);
  }else if (date instanceof ZonedDateTime){
    return (T)((ZonedDateTime)date).plusMonths(months);
  }else if (date instanceof YearMonth){
    return (T)((YearMonth)date).plusMonths(months);
  }
  return (T)date.plus(Duration.of(months, ChronoUnit.MONTHS));
}

相关文章