Java Code Examples for org.threeten.bp.temporal.TemporalField#adjustInto()
The following examples show how to use
org.threeten.bp.temporal.TemporalField#adjustInto() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HijrahDate.java From threetenbp with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public HijrahDate 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 - dayOfWeek.getValue()); 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(yearOfEra, monthOfYear, nvalue); case DAY_OF_YEAR: return resolvePreviousValid(yearOfEra, ((nvalue - 1) / 30) + 1, ((nvalue - 1) % 30) + 1); case EPOCH_DAY: return new HijrahDate(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(yearOfEra, nvalue, dayOfMonth); case YEAR_OF_ERA: return resolvePreviousValid(yearOfEra >= 1 ? nvalue : 1 - nvalue, monthOfYear, dayOfMonth); case YEAR: return resolvePreviousValid(nvalue, monthOfYear, dayOfMonth); case ERA: return resolvePreviousValid(1 - yearOfEra, monthOfYear, dayOfMonth); } throw new UnsupportedTemporalTypeException("Unsupported field: " + field); } return field.adjustInto(this, newValue); }
Example 2
Source File: MinguoDate.java From threetenbp with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public MinguoDate with(TemporalField field, long newValue) { if (field instanceof ChronoField) { ChronoField f = (ChronoField) field; if (getLong(f) == newValue) { return this; } switch (f) { case PROLEPTIC_MONTH: getChronology().range(f).checkValidValue(newValue, f); return plusMonths(newValue - getProlepticMonth()); case YEAR_OF_ERA: case YEAR: case ERA: { int nvalue = getChronology().range(f).checkValidIntValue(newValue, f); switch (f) { case YEAR_OF_ERA: return with(isoDate.withYear(getProlepticYear() >= 1 ? nvalue + YEARS_DIFFERENCE : (1 - nvalue) + YEARS_DIFFERENCE)); case YEAR: return with(isoDate.withYear(nvalue + YEARS_DIFFERENCE)); case ERA: return with(isoDate.withYear((1 - getProlepticYear()) + YEARS_DIFFERENCE)); } } } return with(isoDate.with(field, newValue)); } return field.adjustInto(this, newValue); }
Example 3
Source File: ThaiBuddhistDate.java From threetenbp with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public ThaiBuddhistDate with(TemporalField field, long newValue) { if (field instanceof ChronoField) { ChronoField f = (ChronoField) field; if (getLong(f) == newValue) { return this; } switch (f) { case PROLEPTIC_MONTH: getChronology().range(f).checkValidValue(newValue, f); return plusMonths(newValue - getProlepticMonth()); case YEAR_OF_ERA: case YEAR: case ERA: { int nvalue = getChronology().range(f).checkValidIntValue(newValue, f); switch (f) { case YEAR_OF_ERA: return with(isoDate.withYear((getProlepticYear() >= 1 ? nvalue : 1 - nvalue) - YEARS_DIFFERENCE)); case YEAR: return with(isoDate.withYear(nvalue - YEARS_DIFFERENCE)); case ERA: return with(isoDate.withYear((1 - getProlepticYear()) - YEARS_DIFFERENCE)); } } } return with(isoDate.with(field, newValue)); } return field.adjustInto(this, newValue); }
Example 4
Source File: JapaneseDate.java From threetenbp with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public JapaneseDate with(TemporalField field, long newValue) { if (field instanceof ChronoField) { ChronoField f = (ChronoField) field; if (getLong(f) == newValue) { // validates unsupported fields return this; } switch (f) { case DAY_OF_YEAR: case YEAR_OF_ERA: case ERA: { int nvalue = getChronology().range(f).checkValidIntValue(newValue, f); switch (f) { case DAY_OF_YEAR: return with(isoDate.plusDays(nvalue - getDayOfYear())); case YEAR_OF_ERA: return this.withYear(nvalue); case ERA: { return this.withYear(JapaneseEra.of(nvalue), yearOfEra); } } } } return with(isoDate.with(field, newValue)); } return field.adjustInto(this, newValue); }
Example 5
Source File: YearMonth.java From threetenbp with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * Returns a copy of this year-month with the specified field set to a new value. * <p> * This returns a new {@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 a {@code DateTimeException}. * <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 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 - getLong(PROLEPTIC_MONTH)); 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); }
Example 6
Source File: ZonedDateTime.java From threetenbp with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * 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); }
Example 7
Source File: OffsetTime.java From threetenbp with BSD 3-Clause "New" or "Revised" License | 3 votes |
/** * Returns a copy of this time with the specified field set to a new value. * <p> * This returns a new {@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 a {@code DateTimeException}. * <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 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); }
Example 8
Source File: OffsetDateTime.java From threetenbp with BSD 3-Clause "New" or "Revised" License | 3 votes |
/** * Returns a copy of this date-time with the specified field set to a new value. * <p> * This returns a new {@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 a {@code DateTimeException}. * <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 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); }
Example 9
Source File: LocalDateTime.java From threetenbp with BSD 3-Clause "New" or "Revised" License | 3 votes |
/** * Returns a copy of this date-time with the specified field set to a new value. * <p> * This returns a new {@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 a {@code DateTimeException}. * <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 ArithmeticException if numeric overflow occurs */ @Override public LocalDateTime with(TemporalField field, long newValue) { if (field instanceof ChronoField) { if (field.isTimeBased()) { return with(date, time.with(field, newValue)); } else { return with(date.with(field, newValue), time); } } return field.adjustInto(this, newValue); }
Example 10
Source File: Year.java From threetenbp with BSD 3-Clause "New" or "Revised" License | 3 votes |
/** * Returns a copy of this year with the specified field set to a new value. * <p> * This returns a new {@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 a {@code DateTimeException}. * <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 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); }