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

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

本文整理了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

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

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

  1. /**
  2. * Returns a copy of this {@code OffsetDateTime} with the specified period in years subtracted.
  3. * <p>
  4. * This method subtracts the specified amount from the years field in three steps:
  5. * <ol>
  6. * <li>Subtract the input years to the year field</li>
  7. * <li>Check if the resulting date would be invalid</li>
  8. * <li>Adjust the day-of-month to the last valid day if necessary</li>
  9. * </ol>
  10. * <p>
  11. * For example, 2008-02-29 (leap year) minus one year would result in the
  12. * invalid date 2009-02-29 (standard year). Instead of returning an invalid
  13. * result, the last valid day of the month, 2009-02-28, is selected instead.
  14. * <p>
  15. * This instance is immutable and unaffected by this method call.
  16. *
  17. * @param years the years to subtract, may be negative
  18. * @return an {@code OffsetDateTime} based on this date-time with the years subtracted, not null
  19. * @throws DateTimeException if the result exceeds the supported date range
  20. */
  21. public OffsetDateTime minusYears(long years) {
  22. return (years == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-years));
  23. }

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

  1. /**
  2. * Method adds/subtracts time from the current dateTime.
  3. *
  4. * @param minutes are the minutes.
  5. * @param hours are the hours.
  6. * @param days are the days.
  7. * @param months are the months.
  8. * @param years are the years.
  9. * @return the changed dateTime as String.
  10. */
  11. public static String addTimeToCurrentDateTime(final int minutes, final int hours, final int days, final int months, final int years) {
  12. final OffsetDateTime now = OffsetDateTime.now();
  13. now.plusMinutes(minutes);
  14. now.plusHours(hours);
  15. now.plusDays(days);
  16. now.plusMonths(months);
  17. now.plusYears(years);
  18. return now.toString();
  19. }
  20. }

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

  1. /**
  2. * 年の加算を実行します
  3. *
  4. * @param date
  5. * 日付型
  6. * @param years
  7. * 加算する年
  8. * @return 年を加算した結果のカレンダー
  9. */
  10. @SuppressWarnings("unchecked")
  11. public static <T extends Temporal> T addYears(final T date, final int years) {
  12. if (date == null) {
  13. return null;
  14. }
  15. if (date instanceof LocalDate){
  16. return (T)((LocalDate)date).plusYears(years);
  17. }else if (date instanceof LocalDateTime){
  18. return (T)((LocalDateTime)date).plusYears(years);
  19. }else if (date instanceof OffsetDateTime){
  20. return (T)((OffsetDateTime)date).plusYears(years);
  21. }else if (date instanceof ZonedDateTime){
  22. return (T)((ZonedDateTime)date).plusYears(years);
  23. }else if (date instanceof YearMonth){
  24. return (T)((YearMonth)date).plusYears(years);
  25. }
  26. return (T)date.plus(Duration.of(years, ChronoUnit.YEARS));
  27. }

相关文章