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

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

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

YearMonth.with介绍

[英]Returns a copy of this year-month with the new year and month, checking to see if a new object is in fact required.
[中]返回包含新年和月份的本月副本,检查是否确实需要新对象。

代码示例

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

  1. /**
  2. * Returns a copy of this {@code YearMonth} with the month-of-year altered.
  3. * <p>
  4. * This instance is immutable and unaffected by this method call.
  5. *
  6. * @param month the month-of-year to set in the returned year-month, from 1 (January) to 12 (December)
  7. * @return a {@code YearMonth} based on this year-month with the requested month, not null
  8. * @throws DateTimeException if the month-of-year value is invalid
  9. */
  10. public YearMonth withMonth(int month) {
  11. MONTH_OF_YEAR.checkValidValue(month);
  12. return with(year, month);
  13. }

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

  1. /**
  2. * Returns a copy of this {@code YearMonth} with the year altered.
  3. * <p>
  4. * This instance is immutable and unaffected by this method call.
  5. *
  6. * @param year the year to set in the returned year-month, from MIN_YEAR to MAX_YEAR
  7. * @return a {@code YearMonth} based on this year-month with the requested year, not null
  8. * @throws DateTimeException if the year value is invalid
  9. */
  10. public YearMonth withYear(int year) {
  11. YEAR.checkValidValue(year);
  12. return with(year, month);
  13. }

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

  1. /**
  2. * Returns a copy of this year-month with the specified period in years added.
  3. * <p>
  4. * This instance is immutable and unaffected by this method call.
  5. *
  6. * @param yearsToAdd the years to add, may be negative
  7. * @return a {@code YearMonth} based on this year-month with the years added, not null
  8. * @throws DateTimeException if the result exceeds the supported range
  9. */
  10. public YearMonth plusYears(long yearsToAdd) {
  11. if (yearsToAdd == 0) {
  12. return this;
  13. }
  14. int newYear = YEAR.checkValidIntValue(year + yearsToAdd); // safe overflow
  15. return with(newYear, month);
  16. }

代码示例来源:origin: sakaiproject/sakai

  1. YearMonth jan = currentYearMonth.with(Month.JANUARY);
  2. YearMonth feb = currentYearMonth.with(Month.FEBRUARY);
  3. YearMonth mar = currentYearMonth.with(Month.MARCH);
  4. YearMonth apr = currentYearMonth.with(Month.APRIL);
  5. YearMonth may = currentYearMonth.with(Month.MAY);
  6. YearMonth jun = currentYearMonth.with(Month.JUNE);
  7. YearMonth jul = currentYearMonth.with(Month.JULY);
  8. YearMonth aug = currentYearMonth.with(Month.AUGUST);
  9. YearMonth sep = currentYearMonth.with(Month.SEPTEMBER);
  10. YearMonth oct = currentYearMonth.with(Month.OCTOBER);
  11. YearMonth nov = currentYearMonth.with(Month.NOVEMBER);
  12. YearMonth dec = currentYearMonth.with(Month.DECEMBER);

代码示例来源:origin: org.sakaiproject.calendar/sakai-calendar-util

  1. YearMonth jan = currentYearMonth.with(Month.JANUARY);
  2. YearMonth feb = currentYearMonth.with(Month.FEBRUARY);
  3. YearMonth mar = currentYearMonth.with(Month.MARCH);
  4. YearMonth apr = currentYearMonth.with(Month.APRIL);
  5. YearMonth may = currentYearMonth.with(Month.MAY);
  6. YearMonth jun = currentYearMonth.with(Month.JUNE);
  7. YearMonth jul = currentYearMonth.with(Month.JULY);
  8. YearMonth aug = currentYearMonth.with(Month.AUGUST);
  9. YearMonth sep = currentYearMonth.with(Month.SEPTEMBER);
  10. YearMonth oct = currentYearMonth.with(Month.OCTOBER);
  11. YearMonth nov = currentYearMonth.with(Month.NOVEMBER);
  12. YearMonth dec = currentYearMonth.with(Month.DECEMBER);

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

  1. /**
  2. * Returns a copy of this year-month with the specified period in months added.
  3. * <p>
  4. * This instance is immutable and unaffected by this method call.
  5. *
  6. * @param monthsToAdd the months to add, may be negative
  7. * @return a {@code YearMonth} based on this year-month with the months added, not null
  8. * @throws DateTimeException if the result exceeds the supported range
  9. */
  10. public YearMonth plusMonths(long monthsToAdd) {
  11. if (monthsToAdd == 0) {
  12. return this;
  13. }
  14. long monthCount = year * 12L + (month - 1);
  15. long calcMonths = monthCount + monthsToAdd; // safe overflow
  16. int newYear = YEAR.checkValidIntValue(Jdk8Methods.floorDiv(calcMonths, 12));
  17. int newMonth = Jdk8Methods.floorMod(calcMonths, 12) + 1;
  18. return with(newYear, newMonth);
  19. }

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

相关文章