本文整理汇总了Java中java.time.temporal.TemporalField.adjustInto方法的典型用法代码示例。如果您正苦于以下问题:Java TemporalField.adjustInto方法的具体用法?Java TemporalField.adjustInto怎么用?Java TemporalField.adjustInto使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.temporal.TemporalField
的用法示例。
在下文中一共展示了TemporalField.adjustInto方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: with
import java.time.temporal.TemporalField; //导入方法依赖的package包/类
@Override
public CopticDate with(TemporalField field, long newValue) {
if (field instanceof ChronoField) {
ChronoField f = (ChronoField) field;
f.checkValidValue(newValue); // TODO: validate value
int nvalue = (int) newValue;
switch (f) {
case DAY_OF_WEEK: return plusDays(newValue - get(ChronoField.DAY_OF_WEEK));
case ALIGNED_DAY_OF_WEEK_IN_MONTH: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_MONTH));
case ALIGNED_DAY_OF_WEEK_IN_YEAR: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_YEAR));
case DAY_OF_MONTH: return resolvePreviousValid(prolepticYear, month, nvalue);
case DAY_OF_YEAR: return resolvePreviousValid(prolepticYear, ((nvalue - 1) / 30) + 1, ((nvalue - 1) % 30) + 1);
case EPOCH_DAY: return ofEpochDay(nvalue);
case ALIGNED_WEEK_OF_MONTH: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_MONTH)) * 7);
case ALIGNED_WEEK_OF_YEAR: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_YEAR)) * 7);
case MONTH_OF_YEAR: return resolvePreviousValid(prolepticYear, nvalue, day);
case YEAR_OF_ERA: return resolvePreviousValid(prolepticYear >= 1 ? nvalue : 1 - nvalue, month, day);
case YEAR: return resolvePreviousValid(nvalue, month, day);
case ERA: return resolvePreviousValid(1 - prolepticYear, month, day);
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
}
return field.adjustInto(this, newValue);
}
示例2: test_adjustInto_wby
import java.time.temporal.TemporalField; //导入方法依赖的package包/类
@Test(dataProvider = "fields")
public void test_adjustInto_wby(TemporalField weekField, TemporalField yearField) {
// tests every day from 2012 to 2016 inclusive
LocalDate date = LocalDate.of(2012, 1, 2);
int wby = 2012;
int week = 1;
int dow = 1;
for (int i = 1; i <= ((52 + 52 + 52 + 53 + 52) * 7); i++) {
for (int j = 2004; j <= 2015; j++) {
LocalDate adjusted = yearField.adjustInto(date, j);
assertEquals(adjusted.get(yearField), j);
assertEquals(adjusted.get(DAY_OF_WEEK), dow);
assertEquals(adjusted.get(weekField), (week == 53 && wbyLen(j) == 52 ? 52 : week), "" + date + " " + adjusted);
}
if (dow == 7) {
dow = 1;
week++;
} else {
dow++;
}
if (week > wbyLen(wby)) {
week = 1;
wby++;
}
date = date.plusDays(1);
}
}
示例3: test_adjustInto_week
import java.time.temporal.TemporalField; //导入方法依赖的package包/类
@Test(dataProvider = "fields")
public void test_adjustInto_week(TemporalField weekField, TemporalField yearField) {
// tests every day from 2012 to 2016 inclusive
LocalDate date = LocalDate.of(2012, 1, 2);
int wby = 2012;
int week = 1;
int dow = 1;
for (int i = 1; i <= ((52 + 52 + 52 + 53 + 52) * 7); i++) {
int weeksInYear = (wby == 2015 ? 53 : 52);
for (int j = 1; j <= weeksInYear; j++) {
LocalDate adjusted = weekField.adjustInto(date, j);
assertEquals(adjusted.get(weekField), j);
assertEquals(adjusted.get(DAY_OF_WEEK), dow);
assertEquals(adjusted.get(yearField), wby);
}
if (dow == 7) {
dow = 1;
week++;
} else {
dow++;
}
if (week > wbyLen(wby)) {
week = 1;
wby++;
}
date = date.plusDays(1);
}
}
示例4: with
import java.time.temporal.TemporalField; //导入方法依赖的package包/类
/**
* Returns a copy of this year-month with the specified field set to a new value.
* <p>
* This returns a {@code YearMonth}, based on this one, with the value
* for the specified field changed.
* This can be used to change any supported field, such as the year or month.
* If it is not possible to set the value, because the field is not supported or for
* some other reason, an exception is thrown.
* <p>
* If the field is a {@link ChronoField} then the adjustment is implemented here.
* The supported fields behave as follows:
* <ul>
* <li>{@code MONTH_OF_YEAR} -
* Returns a {@code YearMonth} with the specified month-of-year.
* The year will be unchanged.
* <li>{@code PROLEPTIC_MONTH} -
* Returns a {@code YearMonth} with the specified proleptic-month.
* This completely replaces the year and month of this object.
* <li>{@code YEAR_OF_ERA} -
* Returns a {@code YearMonth} with the specified year-of-era
* The month and era will be unchanged.
* <li>{@code YEAR} -
* Returns a {@code YearMonth} with the specified year.
* The month will be unchanged.
* <li>{@code ERA} -
* Returns a {@code YearMonth} with the specified era.
* The month and year-of-era will be unchanged.
* </ul>
* <p>
* In all cases, if the new value is outside the valid range of values for the field
* then a {@code DateTimeException} will be thrown.
* <p>
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
* passing {@code this} as the argument. In this case, the field determines
* whether and how to adjust the instant.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param field the field to set in the result, not null
* @param newValue the new value of the field in the result
* @return a {@code YearMonth} based on {@code this} with the specified field set, not null
* @throws DateTimeException if the field cannot be set
* @throws UnsupportedTemporalTypeException if the field is not supported
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public YearMonth with(TemporalField field, long newValue) {
if (field instanceof ChronoField) {
ChronoField f = (ChronoField) field;
f.checkValidValue(newValue);
switch (f) {
case MONTH_OF_YEAR: return withMonth((int) newValue);
case PROLEPTIC_MONTH: return plusMonths(newValue - getProlepticMonth());
case YEAR_OF_ERA: return withYear((int) (year < 1 ? 1 - newValue : newValue));
case YEAR: return withYear((int) newValue);
case ERA: return (getLong(ERA) == newValue ? this : withYear(1 - year));
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
}
return field.adjustInto(this, newValue);
}
示例5: with
import java.time.temporal.TemporalField; //导入方法依赖的package包/类
/**
* Returns a copy of this date-time with the specified field set to a new value.
* <p>
* This returns a {@code ZonedDateTime}, based on this one, with the value
* for the specified field changed.
* This can be used to change any supported field, such as the year, month or day-of-month.
* If it is not possible to set the value, because the field is not supported or for
* some other reason, an exception is thrown.
* <p>
* In some cases, changing the specified field can cause the resulting date-time to become invalid,
* such as changing the month from 31st January to February would make the day-of-month invalid.
* In cases like this, the field is responsible for resolving the date. Typically it will choose
* the previous valid date, which would be the last valid day of February in this example.
* <p>
* If the field is a {@link ChronoField} then the adjustment is implemented here.
* <p>
* The {@code INSTANT_SECONDS} field will return a date-time with the specified instant.
* The zone and nano-of-second are unchanged.
* The result will have an offset derived from the new instant and original zone.
* If the new instant value is outside the valid range then a {@code DateTimeException} will be thrown.
* <p>
* The {@code OFFSET_SECONDS} field will typically be ignored.
* The offset of a {@code ZonedDateTime} is controlled primarily by the time-zone.
* As such, changing the offset does not generally make sense, because there is only
* one valid offset for the local date-time and zone.
* If the zoned date-time is in a daylight savings overlap, then the offset is used
* to switch between the two valid offsets. In all other cases, the offset is ignored.
* If the new offset value is outside the valid range then a {@code DateTimeException} will be thrown.
* <p>
* The other {@link #isSupported(TemporalField) supported fields} will behave as per
* the matching method on {@link LocalDateTime#with(TemporalField, long) LocalDateTime}.
* The zone is not part of the calculation and will be unchanged.
* When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
* then the offset will be retained if possible, otherwise the earlier offset will be used.
* If in a gap, the local date-time will be adjusted forward by the length of the gap.
* <p>
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
* passing {@code this} as the argument. In this case, the field determines
* whether and how to adjust the instant.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param field the field to set in the result, not null
* @param newValue the new value of the field in the result
* @return a {@code ZonedDateTime} based on {@code this} with the specified field set, not null
* @throws DateTimeException if the field cannot be set
* @throws UnsupportedTemporalTypeException if the field is not supported
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public ZonedDateTime with(TemporalField field, long newValue) {
if (field instanceof ChronoField) {
ChronoField f = (ChronoField) field;
switch (f) {
case INSTANT_SECONDS:
return create(newValue, getNano(), zone);
case OFFSET_SECONDS:
ZoneOffset offset = ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue));
return resolveOffset(offset);
}
return resolveLocal(dateTime.with(field, newValue));
}
return field.adjustInto(this, newValue);
}
示例6: with
import java.time.temporal.TemporalField; //导入方法依赖的package包/类
/**
* Returns a copy of this time with the specified field set to a new value.
* <p>
* This returns an {@code OffsetTime}, based on this one, with the value
* for the specified field changed.
* This can be used to change any supported field, such as the hour, minute or second.
* If it is not possible to set the value, because the field is not supported or for
* some other reason, an exception is thrown.
* <p>
* If the field is a {@link ChronoField} then the adjustment is implemented here.
* <p>
* The {@code OFFSET_SECONDS} field will return a time with the specified offset.
* The local time is unaltered. If the new offset value is outside the valid range
* then a {@code DateTimeException} will be thrown.
* <p>
* The other {@link #isSupported(TemporalField) supported fields} will behave as per
* the matching method on {@link LocalTime#with(TemporalField, long)} LocalTime}.
* In this case, the offset is not part of the calculation and will be unchanged.
* <p>
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
* passing {@code this} as the argument. In this case, the field determines
* whether and how to adjust the instant.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param field the field to set in the result, not null
* @param newValue the new value of the field in the result
* @return an {@code OffsetTime} based on {@code this} with the specified field set, not null
* @throws DateTimeException if the field cannot be set
* @throws UnsupportedTemporalTypeException if the field is not supported
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public OffsetTime with(TemporalField field, long newValue) {
if (field instanceof ChronoField) {
if (field == OFFSET_SECONDS) {
ChronoField f = (ChronoField) field;
return with(time, ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue)));
}
return with(time.with(field, newValue), offset);
}
return field.adjustInto(this, newValue);
}
示例7: with
import java.time.temporal.TemporalField; //导入方法依赖的package包/类
/**
* Returns a copy of this date-time with the specified field set to a new value.
* <p>
* TThis returns an {@code OffsetDateTime}, based on this one, with the value
* for the specified field changed.
* This can be used to change any supported field, such as the year, month or day-of-month.
* If it is not possible to set the value, because the field is not supported or for
* some other reason, an exception is thrown.
* <p>
* In some cases, changing the specified field can cause the resulting date-time to become invalid,
* such as changing the month from 31st January to February would make the day-of-month invalid.
* In cases like this, the field is responsible for resolving the date. Typically it will choose
* the previous valid date, which would be the last valid day of February in this example.
* <p>
* If the field is a {@link ChronoField} then the adjustment is implemented here.
* <p>
* The {@code INSTANT_SECONDS} field will return a date-time with the specified instant.
* The offset and nano-of-second are unchanged.
* If the new instant value is outside the valid range then a {@code DateTimeException} will be thrown.
* <p>
* The {@code OFFSET_SECONDS} field will return a date-time with the specified offset.
* The local date-time is unaltered. If the new offset value is outside the valid range
* then a {@code DateTimeException} will be thrown.
* <p>
* The other {@link #isSupported(TemporalField) supported fields} will behave as per
* the matching method on {@link LocalDateTime#with(TemporalField, long) LocalDateTime}.
* In this case, the offset is not part of the calculation and will be unchanged.
* <p>
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
* passing {@code this} as the argument. In this case, the field determines
* whether and how to adjust the instant.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param field the field to set in the result, not null
* @param newValue the new value of the field in the result
* @return an {@code OffsetDateTime} based on {@code this} with the specified field set, not null
* @throws DateTimeException if the field cannot be set
* @throws UnsupportedTemporalTypeException if the field is not supported
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public OffsetDateTime with(TemporalField field, long newValue) {
if (field instanceof ChronoField) {
ChronoField f = (ChronoField) field;
switch (f) {
case INSTANT_SECONDS: return ofInstant(Instant.ofEpochSecond(newValue, getNano()), offset);
case OFFSET_SECONDS: {
return with(dateTime, ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue)));
}
}
return with(dateTime.with(field, newValue), offset);
}
return field.adjustInto(this, newValue);
}
示例8: with
import java.time.temporal.TemporalField; //导入方法依赖的package包/类
/**
* Returns a copy of this date-time with the specified field set to a new value.
* <p>
* This returns a {@code LocalDateTime}, based on this one, with the value
* for the specified field changed.
* This can be used to change any supported field, such as the year, month or day-of-month.
* If it is not possible to set the value, because the field is not supported or for
* some other reason, an exception is thrown.
* <p>
* In some cases, changing the specified field can cause the resulting date-time to become invalid,
* such as changing the month from 31st January to February would make the day-of-month invalid.
* In cases like this, the field is responsible for resolving the date. Typically it will choose
* the previous valid date, which would be the last valid day of February in this example.
* <p>
* If the field is a {@link ChronoField} then the adjustment is implemented here.
* The {@link #isSupported(TemporalField) supported fields} will behave as per
* the matching method on {@link LocalDate#with(TemporalField, long) LocalDate}
* or {@link LocalTime#with(TemporalField, long) LocalTime}.
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
* passing {@code this} as the argument. In this case, the field determines
* whether and how to adjust the instant.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param field the field to set in the result, not null
* @param newValue the new value of the field in the result
* @return a {@code LocalDateTime} based on {@code this} with the specified field set, not null
* @throws DateTimeException if the field cannot be set
* @throws UnsupportedTemporalTypeException if the field is not supported
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public LocalDateTime with(TemporalField field, long newValue) {
if (field instanceof ChronoField) {
ChronoField f = (ChronoField) field;
if (f.isTimeBased()) {
return with(date, time.with(field, newValue));
} else {
return with(date.with(field, newValue), time);
}
}
return field.adjustInto(this, newValue);
}
示例9: with
import java.time.temporal.TemporalField; //导入方法依赖的package包/类
/**
* Returns a copy of this date-time with the specified field set to a new value.
* <p>
* This returns an {@code OffsetDateTime}, based on this one, with the value
* for the specified field changed.
* This can be used to change any supported field, such as the year, month or day-of-month.
* If it is not possible to set the value, because the field is not supported or for
* some other reason, an exception is thrown.
* <p>
* In some cases, changing the specified field can cause the resulting date-time to become invalid,
* such as changing the month from 31st January to February would make the day-of-month invalid.
* In cases like this, the field is responsible for resolving the date. Typically it will choose
* the previous valid date, which would be the last valid day of February in this example.
* <p>
* If the field is a {@link ChronoField} then the adjustment is implemented here.
* <p>
* The {@code INSTANT_SECONDS} field will return a date-time with the specified instant.
* The offset and nano-of-second are unchanged.
* If the new instant value is outside the valid range then a {@code DateTimeException} will be thrown.
* <p>
* The {@code OFFSET_SECONDS} field will return a date-time with the specified offset.
* The local date-time is unaltered. If the new offset value is outside the valid range
* then a {@code DateTimeException} will be thrown.
* <p>
* The other {@link #isSupported(TemporalField) supported fields} will behave as per
* the matching method on {@link LocalDateTime#with(TemporalField, long) LocalDateTime}.
* In this case, the offset is not part of the calculation and will be unchanged.
* <p>
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
* passing {@code this} as the argument. In this case, the field determines
* whether and how to adjust the instant.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param field the field to set in the result, not null
* @param newValue the new value of the field in the result
* @return an {@code OffsetDateTime} based on {@code this} with the specified field set, not null
* @throws DateTimeException if the field cannot be set
* @throws UnsupportedTemporalTypeException if the field is not supported
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public OffsetDateTime with(TemporalField field, long newValue) {
if (field instanceof ChronoField) {
ChronoField f = (ChronoField) field;
switch (f) {
case INSTANT_SECONDS: return ofInstant(Instant.ofEpochSecond(newValue, getNano()), offset);
case OFFSET_SECONDS: {
return with(dateTime, ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue)));
}
}
return with(dateTime.with(field, newValue), offset);
}
return field.adjustInto(this, newValue);
}
示例10: with
import java.time.temporal.TemporalField; //导入方法依赖的package包/类
/**
* Returns a copy of this year with the specified field set to a new value.
* <p>
* This returns a {@code Year}, based on this one, with the value
* for the specified field changed.
* If it is not possible to set the value, because the field is not supported or for
* some other reason, an exception is thrown.
* <p>
* If the field is a {@link ChronoField} then the adjustment is implemented here.
* The supported fields behave as follows:
* <ul>
* <li>{@code YEAR_OF_ERA} -
* Returns a {@code Year} with the specified year-of-era
* The era will be unchanged.
* <li>{@code YEAR} -
* Returns a {@code Year} with the specified year.
* This completely replaces the date and is equivalent to {@link #of(int)}.
* <li>{@code ERA} -
* Returns a {@code Year} with the specified era.
* The year-of-era will be unchanged.
* </ul>
* <p>
* In all cases, if the new value is outside the valid range of values for the field
* then a {@code DateTimeException} will be thrown.
* <p>
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
* passing {@code this} as the argument. In this case, the field determines
* whether and how to adjust the instant.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param field the field to set in the result, not null
* @param newValue the new value of the field in the result
* @return a {@code Year} based on {@code this} with the specified field set, not null
* @throws DateTimeException if the field cannot be set
* @throws UnsupportedTemporalTypeException if the field is not supported
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public Year with(TemporalField field, long newValue) {
if (field instanceof ChronoField) {
ChronoField f = (ChronoField) field;
f.checkValidValue(newValue);
switch (f) {
case YEAR_OF_ERA: return Year.of((int) (year < 1 ? 1 - newValue : newValue));
case YEAR: return Year.of((int) newValue);
case ERA: return (getLong(ERA) == newValue ? this : Year.of(1 - year));
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
}
return field.adjustInto(this, newValue);
}