本文整理了Java中java.time.YearMonth.with()
方法的一些代码示例,展示了YearMonth.with()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。YearMonth.with()
方法的具体详情如下:
包路径:java.time.YearMonth
类名称: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
/**
* Returns a copy of this {@code YearMonth} with the month-of-year altered.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param month the month-of-year to set in the returned year-month, from 1 (January) to 12 (December)
* @return a {@code YearMonth} based on this year-month with the requested month, not null
* @throws DateTimeException if the month-of-year value is invalid
*/
public YearMonth withMonth(int month) {
MONTH_OF_YEAR.checkValidValue(month);
return with(year, month);
}
代码示例来源:origin: com.github.seratch/java-time-backport
/**
* Returns a copy of this {@code YearMonth} with the year altered.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param year the year to set in the returned year-month, from MIN_YEAR to MAX_YEAR
* @return a {@code YearMonth} based on this year-month with the requested year, not null
* @throws DateTimeException if the year value is invalid
*/
public YearMonth withYear(int year) {
YEAR.checkValidValue(year);
return with(year, month);
}
代码示例来源:origin: com.github.seratch/java-time-backport
/**
* Returns a copy of this year-month with the specified period in years added.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param yearsToAdd the years to add, may be negative
* @return a {@code YearMonth} based on this year-month with the years added, not null
* @throws DateTimeException if the result exceeds the supported range
*/
public YearMonth plusYears(long yearsToAdd) {
if (yearsToAdd == 0) {
return this;
}
int newYear = YEAR.checkValidIntValue(year + yearsToAdd); // safe overflow
return with(newYear, month);
}
代码示例来源:origin: sakaiproject/sakai
YearMonth jan = currentYearMonth.with(Month.JANUARY);
YearMonth feb = currentYearMonth.with(Month.FEBRUARY);
YearMonth mar = currentYearMonth.with(Month.MARCH);
YearMonth apr = currentYearMonth.with(Month.APRIL);
YearMonth may = currentYearMonth.with(Month.MAY);
YearMonth jun = currentYearMonth.with(Month.JUNE);
YearMonth jul = currentYearMonth.with(Month.JULY);
YearMonth aug = currentYearMonth.with(Month.AUGUST);
YearMonth sep = currentYearMonth.with(Month.SEPTEMBER);
YearMonth oct = currentYearMonth.with(Month.OCTOBER);
YearMonth nov = currentYearMonth.with(Month.NOVEMBER);
YearMonth dec = currentYearMonth.with(Month.DECEMBER);
代码示例来源:origin: org.sakaiproject.calendar/sakai-calendar-util
YearMonth jan = currentYearMonth.with(Month.JANUARY);
YearMonth feb = currentYearMonth.with(Month.FEBRUARY);
YearMonth mar = currentYearMonth.with(Month.MARCH);
YearMonth apr = currentYearMonth.with(Month.APRIL);
YearMonth may = currentYearMonth.with(Month.MAY);
YearMonth jun = currentYearMonth.with(Month.JUNE);
YearMonth jul = currentYearMonth.with(Month.JULY);
YearMonth aug = currentYearMonth.with(Month.AUGUST);
YearMonth sep = currentYearMonth.with(Month.SEPTEMBER);
YearMonth oct = currentYearMonth.with(Month.OCTOBER);
YearMonth nov = currentYearMonth.with(Month.NOVEMBER);
YearMonth dec = currentYearMonth.with(Month.DECEMBER);
代码示例来源:origin: com.github.seratch/java-time-backport
/**
* Returns a copy of this year-month with the specified period in months added.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param monthsToAdd the months to add, may be negative
* @return a {@code YearMonth} based on this year-month with the months added, not null
* @throws DateTimeException if the result exceeds the supported range
*/
public YearMonth plusMonths(long monthsToAdd) {
if (monthsToAdd == 0) {
return this;
}
long monthCount = year * 12L + (month - 1);
long calcMonths = monthCount + monthsToAdd; // safe overflow
int newYear = YEAR.checkValidIntValue(Jdk8Methods.floorDiv(calcMonths, 12));
int newMonth = Jdk8Methods.floorMod(calcMonths, 12) + 1;
return with(newYear, newMonth);
}
代码示例来源:origin: com.github.seratch/java-time-backport
/**
* {@inheritDoc}
* @throws DateTimeException {@inheritDoc}
* @throws ArithmeticException {@inheritDoc}
*/
@Override
public YearMonth plus(long amountToAdd, TemporalUnit unit) {
if (unit instanceof ChronoUnit) {
switch ((ChronoUnit) unit) {
case MONTHS: return plusMonths(amountToAdd);
case YEARS: return plusYears(amountToAdd);
case DECADES: return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 10));
case CENTURIES: return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 100));
case MILLENNIA: return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 1000));
case ERAS: return with(ERA, Jdk8Methods.safeAdd(getLong(ERA), amountToAdd));
}
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
}
return unit.addTo(this, amountToAdd);
}
内容来源于网络,如有侵权,请联系作者删除!