Search in sources :

Example 1 with ChronoField

use of java.time.temporal.ChronoField in project jdk8u_jdk by JetBrains.

the class CopticDate method with.

@Override
public CopticDate with(TemporalField field, long newValue) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        // TODO: validate value
        f.checkValidValue(newValue);
        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);
}
Also used : ChronoField(java.time.temporal.ChronoField) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException)

Example 2 with ChronoField

use of java.time.temporal.ChronoField in project jdk8u_jdk by JetBrains.

the class LocalTime method with.

/**
     * Returns a copy of this time with the specified field set to a new value.
     * <p>
     * This returns a {@code LocalTime}, 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.
     * The supported fields behave as follows:
     * <ul>
     * <li>{@code NANO_OF_SECOND} -
     *  Returns a {@code LocalTime} with the specified nano-of-second.
     *  The hour, minute and second will be unchanged.
     * <li>{@code NANO_OF_DAY} -
     *  Returns a {@code LocalTime} with the specified nano-of-day.
     *  This completely replaces the time and is equivalent to {@link #ofNanoOfDay(long)}.
     * <li>{@code MICRO_OF_SECOND} -
     *  Returns a {@code LocalTime} with the nano-of-second replaced by the specified
     *  micro-of-second multiplied by 1,000.
     *  The hour, minute and second will be unchanged.
     * <li>{@code MICRO_OF_DAY} -
     *  Returns a {@code LocalTime} with the specified micro-of-day.
     *  This completely replaces the time and is equivalent to using {@link #ofNanoOfDay(long)}
     *  with the micro-of-day multiplied by 1,000.
     * <li>{@code MILLI_OF_SECOND} -
     *  Returns a {@code LocalTime} with the nano-of-second replaced by the specified
     *  milli-of-second multiplied by 1,000,000.
     *  The hour, minute and second will be unchanged.
     * <li>{@code MILLI_OF_DAY} -
     *  Returns a {@code LocalTime} with the specified milli-of-day.
     *  This completely replaces the time and is equivalent to using {@link #ofNanoOfDay(long)}
     *  with the milli-of-day multiplied by 1,000,000.
     * <li>{@code SECOND_OF_MINUTE} -
     *  Returns a {@code LocalTime} with the specified second-of-minute.
     *  The hour, minute and nano-of-second will be unchanged.
     * <li>{@code SECOND_OF_DAY} -
     *  Returns a {@code LocalTime} with the specified second-of-day.
     *  The nano-of-second will be unchanged.
     * <li>{@code MINUTE_OF_HOUR} -
     *  Returns a {@code LocalTime} with the specified minute-of-hour.
     *  The hour, second-of-minute and nano-of-second will be unchanged.
     * <li>{@code MINUTE_OF_DAY} -
     *  Returns a {@code LocalTime} with the specified minute-of-day.
     *  The second-of-minute and nano-of-second will be unchanged.
     * <li>{@code HOUR_OF_AMPM} -
     *  Returns a {@code LocalTime} with the specified hour-of-am-pm.
     *  The AM/PM, minute-of-hour, second-of-minute and nano-of-second will be unchanged.
     * <li>{@code CLOCK_HOUR_OF_AMPM} -
     *  Returns a {@code LocalTime} with the specified clock-hour-of-am-pm.
     *  The AM/PM, minute-of-hour, second-of-minute and nano-of-second will be unchanged.
     * <li>{@code HOUR_OF_DAY} -
     *  Returns a {@code LocalTime} with the specified hour-of-day.
     *  The minute-of-hour, second-of-minute and nano-of-second will be unchanged.
     * <li>{@code CLOCK_HOUR_OF_DAY} -
     *  Returns a {@code LocalTime} with the specified clock-hour-of-day.
     *  The minute-of-hour, second-of-minute and nano-of-second will be unchanged.
     * <li>{@code AMPM_OF_DAY} -
     *  Returns a {@code LocalTime} with the specified AM/PM.
     *  The hour-of-am-pm, minute-of-hour, second-of-minute and nano-of-second 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 LocalTime} 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 LocalTime with(TemporalField field, long newValue) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        f.checkValidValue(newValue);
        switch(f) {
            case NANO_OF_SECOND:
                return withNano((int) newValue);
            case NANO_OF_DAY:
                return LocalTime.ofNanoOfDay(newValue);
            case MICRO_OF_SECOND:
                return withNano((int) newValue * 1000);
            case MICRO_OF_DAY:
                return LocalTime.ofNanoOfDay(newValue * 1000);
            case MILLI_OF_SECOND:
                return withNano((int) newValue * 1000_000);
            case MILLI_OF_DAY:
                return LocalTime.ofNanoOfDay(newValue * 1000_000);
            case SECOND_OF_MINUTE:
                return withSecond((int) newValue);
            case SECOND_OF_DAY:
                return plusSeconds(newValue - toSecondOfDay());
            case MINUTE_OF_HOUR:
                return withMinute((int) newValue);
            case MINUTE_OF_DAY:
                return plusMinutes(newValue - (hour * 60 + minute));
            case HOUR_OF_AMPM:
                return plusHours(newValue - (hour % 12));
            case CLOCK_HOUR_OF_AMPM:
                return plusHours((newValue == 12 ? 0 : newValue) - (hour % 12));
            case HOUR_OF_DAY:
                return withHour((int) newValue);
            case CLOCK_HOUR_OF_DAY:
                return withHour((int) (newValue == 24 ? 0 : newValue));
            case AMPM_OF_DAY:
                return plusHours((newValue - (hour / 12)) * 12);
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.adjustInto(this, newValue);
}
Also used : ChronoField(java.time.temporal.ChronoField) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException)

Example 3 with ChronoField

use of java.time.temporal.ChronoField in project jdk8u_jdk by JetBrains.

the class LocalDate method with.

/**
     * Returns a copy of this date with the specified field set to a new value.
     * <p>
     * This returns a {@code LocalDate}, 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 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 supported fields behave as follows:
     * <ul>
     * <li>{@code DAY_OF_WEEK} -
     *  Returns a {@code LocalDate} with the specified day-of-week.
     *  The date is adjusted up to 6 days forward or backward within the boundary
     *  of a Monday to Sunday week.
     * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH} -
     *  Returns a {@code LocalDate} with the specified aligned-day-of-week.
     *  The date is adjusted to the specified month-based aligned-day-of-week.
     *  Aligned weeks are counted such that the first week of a given month starts
     *  on the first day of that month.
     *  This may cause the date to be moved up to 6 days into the following month.
     * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR} -
     *  Returns a {@code LocalDate} with the specified aligned-day-of-week.
     *  The date is adjusted to the specified year-based aligned-day-of-week.
     *  Aligned weeks are counted such that the first week of a given year starts
     *  on the first day of that year.
     *  This may cause the date to be moved up to 6 days into the following year.
     * <li>{@code DAY_OF_MONTH} -
     *  Returns a {@code LocalDate} with the specified day-of-month.
     *  The month and year will be unchanged. If the day-of-month is invalid for the
     *  year and month, then a {@code DateTimeException} is thrown.
     * <li>{@code DAY_OF_YEAR} -
     *  Returns a {@code LocalDate} with the specified day-of-year.
     *  The year will be unchanged. If the day-of-year is invalid for the
     *  year, then a {@code DateTimeException} is thrown.
     * <li>{@code EPOCH_DAY} -
     *  Returns a {@code LocalDate} with the specified epoch-day.
     *  This completely replaces the date and is equivalent to {@link #ofEpochDay(long)}.
     * <li>{@code ALIGNED_WEEK_OF_MONTH} -
     *  Returns a {@code LocalDate} with the specified aligned-week-of-month.
     *  Aligned weeks are counted such that the first week of a given month starts
     *  on the first day of that month.
     *  This adjustment moves the date in whole week chunks to match the specified week.
     *  The result will have the same day-of-week as this date.
     *  This may cause the date to be moved into the following month.
     * <li>{@code ALIGNED_WEEK_OF_YEAR} -
     *  Returns a {@code LocalDate} with the specified aligned-week-of-year.
     *  Aligned weeks are counted such that the first week of a given year starts
     *  on the first day of that year.
     *  This adjustment moves the date in whole week chunks to match the specified week.
     *  The result will have the same day-of-week as this date.
     *  This may cause the date to be moved into the following year.
     * <li>{@code MONTH_OF_YEAR} -
     *  Returns a {@code LocalDate} with the specified month-of-year.
     *  The year will be unchanged. The day-of-month will also be unchanged,
     *  unless it would be invalid for the new month and year. In that case, the
     *  day-of-month is adjusted to the maximum valid value for the new month and year.
     * <li>{@code PROLEPTIC_MONTH} -
     *  Returns a {@code LocalDate} with the specified proleptic-month.
     *  The day-of-month will be unchanged, unless it would be invalid for the new month
     *  and year. In that case, the day-of-month is adjusted to the maximum valid value
     *  for the new month and year.
     * <li>{@code YEAR_OF_ERA} -
     *  Returns a {@code LocalDate} with the specified year-of-era.
     *  The era and month will be unchanged. The day-of-month will also be unchanged,
     *  unless it would be invalid for the new month and year. In that case, the
     *  day-of-month is adjusted to the maximum valid value for the new month and year.
     * <li>{@code YEAR} -
     *  Returns a {@code LocalDate} with the specified year.
     *  The month will be unchanged. The day-of-month will also be unchanged,
     *  unless it would be invalid for the new month and year. In that case, the
     *  day-of-month is adjusted to the maximum valid value for the new month and year.
     * <li>{@code ERA} -
     *  Returns a {@code LocalDate} with the specified era.
     *  The year-of-era and month will be unchanged. The day-of-month will also be unchanged,
     *  unless it would be invalid for the new month and year. In that case, the
     *  day-of-month is adjusted to the maximum valid value for the new month and year.
     * </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 LocalDate} 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 LocalDate with(TemporalField field, long newValue) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        f.checkValidValue(newValue);
        switch(f) {
            case DAY_OF_WEEK:
                return plusDays(newValue - getDayOfWeek().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 withDayOfMonth((int) newValue);
            case DAY_OF_YEAR:
                return withDayOfYear((int) newValue);
            case EPOCH_DAY:
                return LocalDate.ofEpochDay(newValue);
            case ALIGNED_WEEK_OF_MONTH:
                return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_MONTH));
            case ALIGNED_WEEK_OF_YEAR:
                return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_YEAR));
            case MONTH_OF_YEAR:
                return withMonth((int) newValue);
            case PROLEPTIC_MONTH:
                return plusMonths(newValue - getProlepticMonth());
            case YEAR_OF_ERA:
                return withYear((int) (year >= 1 ? newValue : 1 - 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);
}
Also used : ChronoField(java.time.temporal.ChronoField) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException)

Example 4 with ChronoField

use of java.time.temporal.ChronoField in project jdk8u_jdk by JetBrains.

the class DateTimePrintContext method adjust.

private static TemporalAccessor adjust(final TemporalAccessor temporal, DateTimeFormatter formatter) {
    // normal case first (early return is an optimization)
    Chronology overrideChrono = formatter.getChronology();
    ZoneId overrideZone = formatter.getZone();
    if (overrideChrono == null && overrideZone == null) {
        return temporal;
    }
    // ensure minimal change (early return is an optimization)
    Chronology temporalChrono = temporal.query(TemporalQueries.chronology());
    ZoneId temporalZone = temporal.query(TemporalQueries.zoneId());
    if (Objects.equals(overrideChrono, temporalChrono)) {
        overrideChrono = null;
    }
    if (Objects.equals(overrideZone, temporalZone)) {
        overrideZone = null;
    }
    if (overrideChrono == null && overrideZone == null) {
        return temporal;
    }
    // make adjustment
    final Chronology effectiveChrono = (overrideChrono != null ? overrideChrono : temporalChrono);
    if (overrideZone != null) {
        // if have zone and instant, calculation is simple, defaulting chrono if necessary
        if (temporal.isSupported(INSTANT_SECONDS)) {
            Chronology chrono = (effectiveChrono != null ? effectiveChrono : IsoChronology.INSTANCE);
            return chrono.zonedDateTime(Instant.from(temporal), overrideZone);
        }
        // block changing zone on OffsetTime, and similar problem cases
        if (overrideZone.normalized() instanceof ZoneOffset && temporal.isSupported(OFFSET_SECONDS) && temporal.get(OFFSET_SECONDS) != overrideZone.getRules().getOffset(Instant.EPOCH).getTotalSeconds()) {
            throw new DateTimeException("Unable to apply override zone '" + overrideZone + "' because the temporal object being formatted has a different offset but" + " does not represent an instant: " + temporal);
        }
    }
    final ZoneId effectiveZone = (overrideZone != null ? overrideZone : temporalZone);
    final ChronoLocalDate effectiveDate;
    if (overrideChrono != null) {
        if (temporal.isSupported(EPOCH_DAY)) {
            effectiveDate = effectiveChrono.date(temporal);
        } else {
            // check for date fields other than epoch-day, ignoring case of converting null to ISO
            if (!(overrideChrono == IsoChronology.INSTANCE && temporalChrono == null)) {
                for (ChronoField f : ChronoField.values()) {
                    if (f.isDateBased() && temporal.isSupported(f)) {
                        throw new DateTimeException("Unable to apply override chronology '" + overrideChrono + "' because the temporal object being formatted contains date fields but" + " does not represent a whole date: " + temporal);
                    }
                }
            }
            effectiveDate = null;
        }
    } else {
        effectiveDate = null;
    }
    // this better handles map-like underlying temporal instances
    return new TemporalAccessor() {

        @Override
        public boolean isSupported(TemporalField field) {
            if (effectiveDate != null && field.isDateBased()) {
                return effectiveDate.isSupported(field);
            }
            return temporal.isSupported(field);
        }

        @Override
        public ValueRange range(TemporalField field) {
            if (effectiveDate != null && field.isDateBased()) {
                return effectiveDate.range(field);
            }
            return temporal.range(field);
        }

        @Override
        public long getLong(TemporalField field) {
            if (effectiveDate != null && field.isDateBased()) {
                return effectiveDate.getLong(field);
            }
            return temporal.getLong(field);
        }

        @SuppressWarnings("unchecked")
        @Override
        public <R> R query(TemporalQuery<R> query) {
            if (query == TemporalQueries.chronology()) {
                return (R) effectiveChrono;
            }
            if (query == TemporalQueries.zoneId()) {
                return (R) effectiveZone;
            }
            if (query == TemporalQueries.precision()) {
                return temporal.query(query);
            }
            return query.queryFrom(this);
        }
    };
}
Also used : ChronoLocalDate(java.time.chrono.ChronoLocalDate) DateTimeException(java.time.DateTimeException) TemporalAccessor(java.time.temporal.TemporalAccessor) TemporalField(java.time.temporal.TemporalField) ZoneId(java.time.ZoneId) ChronoField(java.time.temporal.ChronoField) TemporalQuery(java.time.temporal.TemporalQuery) Chronology(java.time.chrono.Chronology) IsoChronology(java.time.chrono.IsoChronology) ZoneOffset(java.time.ZoneOffset)

Example 5 with ChronoField

use of java.time.temporal.ChronoField in project jdk8u_jdk by JetBrains.

the class Parsed method resolveTimeLenient.

private void resolveTimeLenient() {
    if (time == null) {
        // NANO_OF_SECOND merged with MILLI/MICRO above
        if (fieldValues.containsKey(MILLI_OF_SECOND)) {
            long los = fieldValues.remove(MILLI_OF_SECOND);
            if (fieldValues.containsKey(MICRO_OF_SECOND)) {
                // merge milli-of-second and micro-of-second for better error message
                long cos = los * 1_000 + (fieldValues.get(MICRO_OF_SECOND) % 1_000);
                updateCheckConflict(MILLI_OF_SECOND, MICRO_OF_SECOND, cos);
                fieldValues.remove(MICRO_OF_SECOND);
                fieldValues.put(NANO_OF_SECOND, cos * 1_000L);
            } else {
                // convert milli-of-second to nano-of-second
                fieldValues.put(NANO_OF_SECOND, los * 1_000_000L);
            }
        } else if (fieldValues.containsKey(MICRO_OF_SECOND)) {
            // convert micro-of-second to nano-of-second
            long cos = fieldValues.remove(MICRO_OF_SECOND);
            fieldValues.put(NANO_OF_SECOND, cos * 1_000L);
        }
        // merge hour/minute/second/nano leniently
        Long hod = fieldValues.get(HOUR_OF_DAY);
        if (hod != null) {
            Long moh = fieldValues.get(MINUTE_OF_HOUR);
            Long som = fieldValues.get(SECOND_OF_MINUTE);
            Long nos = fieldValues.get(NANO_OF_SECOND);
            // check for invalid combinations that cannot be defaulted
            if ((moh == null && (som != null || nos != null)) || (moh != null && som == null && nos != null)) {
                return;
            }
            // default as necessary and build time
            long mohVal = (moh != null ? moh : 0);
            long somVal = (som != null ? som : 0);
            long nosVal = (nos != null ? nos : 0);
            resolveTime(hod, mohVal, somVal, nosVal);
            fieldValues.remove(HOUR_OF_DAY);
            fieldValues.remove(MINUTE_OF_HOUR);
            fieldValues.remove(SECOND_OF_MINUTE);
            fieldValues.remove(NANO_OF_SECOND);
        }
    }
    // validate remaining
    if (resolverStyle != ResolverStyle.LENIENT && fieldValues.size() > 0) {
        for (Entry<TemporalField, Long> entry : fieldValues.entrySet()) {
            TemporalField field = entry.getKey();
            if (field instanceof ChronoField && field.isTimeBased()) {
                ((ChronoField) field).checkValidValue(entry.getValue());
            }
        }
    }
}
Also used : TemporalField(java.time.temporal.TemporalField) ChronoField(java.time.temporal.ChronoField)

Aggregations

ChronoField (java.time.temporal.ChronoField)10 UnsupportedTemporalTypeException (java.time.temporal.UnsupportedTemporalTypeException)8 TemporalField (java.time.temporal.TemporalField)2 DateTimeException (java.time.DateTimeException)1 ZoneId (java.time.ZoneId)1 ZoneOffset (java.time.ZoneOffset)1 ChronoLocalDate (java.time.chrono.ChronoLocalDate)1 Chronology (java.time.chrono.Chronology)1 IsoChronology (java.time.chrono.IsoChronology)1 TemporalAccessor (java.time.temporal.TemporalAccessor)1 TemporalQuery (java.time.temporal.TemporalQuery)1 Calendar (java.util.Calendar)1 LocalGregorianCalendar (sun.util.calendar.LocalGregorianCalendar)1