@Override public void serialize(Year year, JsonGenerator generator, SerializerProvider provider) throws IOException { if (useTimestamp(provider)) { generator.writeNumber(year.getValue()); } else { String str = (_formatter == null) ? year.toString() : year.format(_formatter); generator.writeString(str); } }
static Year readExternal(DataInput in) throws IOException { return Year.of(in.readInt()); }
/** * Obtains an instance of {@code Year} from a text string such as {@code 2007}. * <p> * The string must represent a valid year. * Years outside the range 0000 to 9999 must be prefixed by the plus or minus symbol. * * @param text the text to parse such as "2007", not null * @return the parsed year, not null * @throws DateTimeParseException if the text cannot be parsed */ public static Year parse(CharSequence text) { return parse(text, PARSER); }
Year thisYear = Year.from( today ); Year nextYear = thisYear.plusYears( 1 ); Year lastYear = thisYear.minusYears( 1 );
/** * Gets the value of the specified field from this year as an {@code int}. * <p> * This queries this year for the value for the specified field. * The returned value will always be within the valid range of values for the field. * If it is not possible to return 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 query is implemented here. * The {@link #isSupported(TemporalField) supported fields} will return valid * values based on this year. * 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.getFrom(TemporalAccessor)} * passing {@code this} as the argument. Whether the value can be obtained, * and what the value represents, is determined by the field. * * @param field the field to get, not null * @return the value for the field * @throws DateTimeException if a value for the field cannot be obtained * @throws ArithmeticException if numeric overflow occurs */ @Override // override for Javadoc public int get(TemporalField field) { return range(field).checkValidIntValue(getLong(field), field); }
/** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override public Year plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { switch ((ChronoUnit) unit) { 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); }
@Override public Year deserialize(JsonParser parser, DeserializationContext context) throws IOException { JsonToken t = parser.getCurrentToken(); if (t == JsonToken.VALUE_STRING) { String string = parser.getValueAsString().trim(); try { if (_formatter == null) { return Year.parse(string); } return Year.parse(string, _formatter); } catch (DateTimeException e) { _rethrowDateTimeException(parser, context, e, string); } } if (t == JsonToken.VALUE_NUMBER_INT) { return Year.of(parser.getIntValue()); } if (t == JsonToken.VALUE_EMBEDDED_OBJECT) { return (Year) parser.getEmbeddedObject(); } throw context.mappingException("Unexpected token (%s), expected VALUE_STRING or VALUE_NUMBER_INT", parser.getCurrentToken()); } }
/** * Gets the length of this year in days. * * @return the length of this year in days, 365 or 366 */ public int length() { return isLeap() ? 366 : 365; }
/** * Returns a copy of this year with the specified number of years subtracted. * <p> * This instance is immutable and unaffected by this method call. * * @param yearsToSubtract the years to subtract, may be negative * @return a {@code Year} based on this year with the period subtracted, not null * @throws DateTimeException if the result exceeds the supported year range */ public Year minusYears(long yearsToSubtract) { return (yearsToSubtract == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-yearsToSubtract)); }
ZoneRulesBuilder addToBuilder(ZoneRulesBuilder bld, Map<String, List<TZDBRule>> rules) { if (year != null) { bld.addWindow(standardOffset, toDateTime(year.getValue()), timeDefinition); } else { bld.addWindowForever(standardOffset); } if (fixedSavingsSecs != null) { bld.setFixedSavingsToWindow(fixedSavingsSecs); } else { List<TZDBRule> tzdbRules = rules.get(savingsRule); if (tzdbRules == null) { throw new IllegalArgumentException("Rule not found: " + savingsRule); } for (TZDBRule tzdbRule : tzdbRules) { tzdbRule.addToBuilder(bld); } } return bld; }
@Override public Year queryFrom(TemporalAccessor temporal) { return Year.from(temporal); } };
/** * Obtains an instance of {@code Year}. * <p> * This method accepts a year value from the proleptic ISO calendar system. * <p> * The year 2AD/CE is represented by 2.<br> * The year 1AD/CE is represented by 1.<br> * The year 1BC/BCE is represented by 0.<br> * The year 2BC/BCE is represented by -1.<br> * * @param isoYear the ISO proleptic year to represent, from {@code MIN_VALUE} to {@code MAX_VALUE} * @return the year, not null * @throws DateTimeException if the field is invalid */ public static Year of(int isoYear) { YEAR.checkValidValue(isoYear); return new Year(isoYear); }
/** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override public Year plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { switch ((ChronoUnit) unit) { 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); }
/** * Gets the length of this year in days. * * @return the length of this year in days, 365 or 366 */ public int length() { return isLeap() ? 366 : 365; }
/** * Returns a copy of this year with the specified number of years subtracted. * <p> * This instance is immutable and unaffected by this method call. * * @param yearsToSubtract the years to subtract, may be negative * @return a {@code Year} based on this year with the period subtracted, not null * @throws DateTimeException if the result exceeds the supported year range */ public Year minusYears(long yearsToSubtract) { return (yearsToSubtract == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-yearsToSubtract)); }
ZoneRulesBuilder addToBuilder(ZoneRulesBuilder bld, Map<String, List<TZDBRule>> rules) { if (year != null) { bld.addWindow(standardOffset, toDateTime(year.getValue()), timeDefinition); } else { bld.addWindowForever(standardOffset); } if (fixedSavingsSecs != null) { bld.setFixedSavingsToWindow(fixedSavingsSecs); } else { List<TZDBRule> tzdbRules = rules.get(savingsRule); if (tzdbRules == null) { throw new IllegalArgumentException("Rule not found: " + savingsRule); } for (TZDBRule tzdbRule : tzdbRules) { tzdbRule.addToBuilder(bld); } } return bld; }
/** * Gets the value of the specified field from this year as an {@code int}. * <p> * This queries this year for the value for the specified field. * The returned value will always be within the valid range of values for the field. * If it is not possible to return 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 query is implemented here. * The {@link #isSupported(TemporalField) supported fields} will return valid * values based on this year. * 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.getFrom(TemporalAccessor)} * passing {@code this} as the argument. Whether the value can be obtained, * and what the value represents, is determined by the field. * * @param field the field to get, not null * @return the value for the field * @throws DateTimeException if a value for the field cannot be obtained * @throws ArithmeticException if numeric overflow occurs */ @Override // override for Javadoc public int get(TemporalField field) { return range(field).checkValidIntValue(getLong(field), field); }