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

x33g5p2x  于2022-02-05 转载在 其他  
字(3.3k)|赞(0)|评价(0)|浏览(126)

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

YearMonth.plusYears介绍

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

This instance is immutable and unaffected by this method call.
[中]返回添加了指定期间(以年为单位)的本年月份的副本。
此实例是不可变的,不受此方法调用的影响。

代码示例

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

  1. /**
  2. * Returns a copy of this year-month with the specified period in years subtracted.
  3. * <p>
  4. * This instance is immutable and unaffected by this method call.
  5. *
  6. * @param yearsToSubtract the years to subtract, may be negative
  7. * @return a {@code YearMonth} based on this year-month with the years subtracted, not null
  8. * @throws DateTimeException if the result exceeds the supported range
  9. */
  10. public YearMonth minusYears(long yearsToSubtract) {
  11. return (yearsToSubtract == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-yearsToSubtract));
  12. }

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

  1. /**
  2. * 年の加算を実行します
  3. *
  4. * @param date
  5. * 日付型
  6. * @param years
  7. * 加算する年
  8. * @return 年を加算した結果のカレンダー
  9. */
  10. public static YearMonth addYears(final YearMonth date, final int years) {
  11. if (date == null) {
  12. return null;
  13. }
  14. return date.plusYears(years);
  15. }

代码示例来源:origin: com.github.lgooddatepicker/LGoodDatePicker

  1. /**
  2. * buttonNextYearActionPerformed, This event is called when the next year button is pressed.
  3. * This sets the YearMonth of the calendar to the next year, and redraws the calendar.
  4. */
  5. private void buttonNextYearActionPerformed(ActionEvent e) {
  6. // We catch and ignore any exceptions at the minimum and maximum of the local date range.
  7. try {
  8. drawCalendar(displayedYearMonth.plusYears(1));
  9. } catch (Exception ex) {
  10. }
  11. }

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

  1. /**
  2. * {@inheritDoc}
  3. * @throws DateTimeException {@inheritDoc}
  4. * @throws ArithmeticException {@inheritDoc}
  5. */
  6. @Override
  7. public YearMonth plus(long amountToAdd, TemporalUnit unit) {
  8. if (unit instanceof ChronoUnit) {
  9. switch ((ChronoUnit) unit) {
  10. case MONTHS: return plusMonths(amountToAdd);
  11. case YEARS: return plusYears(amountToAdd);
  12. case DECADES: return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 10));
  13. case CENTURIES: return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 100));
  14. case MILLENNIA: return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 1000));
  15. case ERAS: return with(ERA, Jdk8Methods.safeAdd(getLong(ERA), amountToAdd));
  16. }
  17. throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
  18. }
  19. return unit.addTo(this, amountToAdd);
  20. }

代码示例来源:origin: com.github.lgooddatepicker/LGoodDatePicker

  1. YearMonth choiceYearMonth = displayedYearMonth.plusYears(yearDifference);
  2. String choiceYearMonthString = "" + choiceYearMonth.getYear();
  3. yearPopupMenu.add(new JMenuItem(new AbstractAction(choiceYearMonthString) {

代码示例来源: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. }

相关文章