public static long truncate(Instant instant, ChronoUnit unit) { switch (unit) { case NANOS: case MICROS: case MILLIS: case SECONDS: case MINUTES: case HOURS: case HALF_DAYS: case DAYS: return instant.atZone(systemDefault()).truncatedTo(unit).toEpochSecond() * 1000; case MONTHS: return instant.atZone(systemDefault()).truncatedTo(DAYS).withDayOfMonth(1).toEpochSecond() * 1000; case YEARS: return instant.atZone(systemDefault()).truncatedTo(DAYS).withDayOfYear(1).toEpochSecond() * 1000; default: throw new UnsupportedTemporalTypeException("Invalid unit for truncation: " + unit); } }
public static ZonedDateTime truncate(ZonedDateTime time, ChronoUnit unit) { switch (unit) { case NANOS: case MICROS: case MILLIS: case SECONDS: case MINUTES: case HOURS: case HALF_DAYS: case DAYS: return time.truncatedTo(unit); case MONTHS: return time.truncatedTo(DAYS).withDayOfMonth(1); case YEARS: return time.truncatedTo(DAYS).withDayOfYear(1); default: throw new UnsupportedTemporalTypeException("Invalid unit for truncation: " + unit); } }
/** * Renders a Temporal value type Field * @param property Property to render * @return Field instance */ @SuppressWarnings("unchecked") protected Field<T> renderTemporal(Property<T> property) { TemporalInputBuilder builder = null; if (LocalDate.class.isAssignableFrom(property.getType())) { builder = input.localDate(false); } else if (LocalDateTime.class.isAssignableFrom(property.getType())) { builder = input.localDateTime(false); } else { throw new UnsupportedTemporalTypeException( "Temporal type " + property.getType().getName() + " is not supported by default field renderer"); } final TemporalInputBuilder<Temporal, ?> b = builder; // set locale from LocalizationContext, if any LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap((c) -> c.getLocale()) .ifPresent((l) -> b.locale(l)); return postProcessField(b.asField(), property); }
private int get0(TemporalField field) { switch ((ChronoField) field) { case DAY_OF_WEEK: return getDayOfWeek().getValue(); case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((day - 1) % 7) + 1; case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1; case DAY_OF_MONTH: return day; case DAY_OF_YEAR: return getDayOfYear(); case EPOCH_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'EpochDay' for get() method, use getLong() instead"); case ALIGNED_WEEK_OF_MONTH: return ((day - 1) / 7) + 1; case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1; case MONTH_OF_YEAR: return month; case PROLEPTIC_MONTH: throw new UnsupportedTemporalTypeException("Invalid field 'ProlepticMonth' for get() method, use getLong() instead"); case YEAR_OF_ERA: return (year >= 1 ? year : 1 - year); case YEAR: return year; case ERA: return (year >= 1 ? 1 : 0); } throw new UnsupportedTemporalTypeException("Unsupported field: " + field); }
@Override public ValueRange range(TemporalField field) { if (field instanceof ChronoField) { if (isSupported(field)) { ChronoField f = (ChronoField) field; switch (f) { case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth()); case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear()); case YEAR_OF_ERA: { Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE); jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET); jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth()); return ValueRange.of(1, jcal.getActualMaximum(Calendar.YEAR)); } } return getChronology().range(f); } throw new UnsupportedTemporalTypeException("Unsupported field: " + field); } return field.rangeRefinedBy(this); }
@Override public ValueRange range(TemporalField field) { if (field instanceof ChronoField) { if (isSupported(field)) { ChronoField f = (ChronoField) field; switch (f) { case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth()); case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear()); case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, 5); // TODO // TODO does the limited range of valid years cause years to // start/end part way through? that would affect range } return getChronology().range(f); } throw new UnsupportedTemporalTypeException("Unsupported field: " + field); } return field.rangeRefinedBy(this); }
@Override public long getLong(TemporalField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case DAY_OF_WEEK: return getDayOfWeek(); case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((getDayOfWeek() - 1) % 7) + 1; case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1; case DAY_OF_MONTH: return this.dayOfMonth; case DAY_OF_YEAR: return this.getDayOfYear(); case EPOCH_DAY: return toEpochDay(); case ALIGNED_WEEK_OF_MONTH: return ((dayOfMonth - 1) / 7) + 1; case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1; case MONTH_OF_YEAR: return monthOfYear; case PROLEPTIC_MONTH: return getProlepticMonth(); case YEAR_OF_ERA: return prolepticYear; case YEAR: return prolepticYear; case ERA: return getEraValue(); } throw new UnsupportedTemporalTypeException("Unsupported field: " + field); } return field.getFrom(this); }
@Override public HijrahDate with(TemporalField field, long newValue) { if (field instanceof ChronoField) { ChronoField f = (ChronoField) field; // not using checkValidIntValue so EPOCH_DAY and PROLEPTIC_MONTH work chrono.range(f).checkValidValue(newValue, f); // TODO: validate value int nvalue = (int) newValue; switch (f) { case DAY_OF_WEEK: return plusDays(newValue - getDayOfWeek()); 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, monthOfYear, nvalue); case DAY_OF_YEAR: return plusDays(Math.min(nvalue, lengthOfYear()) - getDayOfYear()); case EPOCH_DAY: return new HijrahDate(chrono, newValue); 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, dayOfMonth); case PROLEPTIC_MONTH: return plusMonths(newValue - getProlepticMonth()); case YEAR_OF_ERA: return resolvePreviousValid(prolepticYear >= 1 ? nvalue : 1 - nvalue, monthOfYear, dayOfMonth); case YEAR: return resolvePreviousValid(nvalue, monthOfYear, dayOfMonth); case ERA: return resolvePreviousValid(1 - prolepticYear, monthOfYear, dayOfMonth); } throw new UnsupportedTemporalTypeException("Unsupported field: " + field); } return super.with(field, newValue); }
@Override @SuppressWarnings("unchecked") public D plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; switch (f) { case DAYS: return plusDays(amountToAdd); case WEEKS: return plusDays(Math.multiplyExact(amountToAdd, 7)); case MONTHS: return plusMonths(amountToAdd); case YEARS: return plusYears(amountToAdd); case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10)); case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100)); case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000)); case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd)); } throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } return (D) ChronoLocalDate.super.plus(amountToAdd, unit); }
@Override public long until(Temporal endExclusive, TemporalUnit unit) { Objects.requireNonNull(endExclusive, "endExclusive"); ChronoLocalDate end = getChronology().date(endExclusive); if (unit instanceof ChronoUnit) { switch ((ChronoUnit) unit) { case DAYS: return daysUntil(end); case WEEKS: return daysUntil(end) / 7; case MONTHS: return monthsUntil(end); case YEARS: return monthsUntil(end) / 12; case DECADES: return monthsUntil(end) / 120; case CENTURIES: return monthsUntil(end) / 1200; case MILLENNIA: return monthsUntil(end) / 12000; case ERAS: return end.getLong(ERA) - getLong(ERA); } throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } Objects.requireNonNull(unit, "unit"); return unit.between(this, end); }
public DateCalculatorResult calculate(String text) { final DateCalcExpressionParser parser = new DateCalcExpressionParser(); final Queue<Token> tokens = parser.parse(text); try { if (!tokens.isEmpty()) { if (tokens.peek() instanceof DateToken) { return handleDateExpression(tokens); } else if (tokens.peek() instanceof TimeToken) { return handleTimeExpression(tokens); } } } catch (UnsupportedTemporalTypeException utte) { throw new DateCalcException(utte.getLocalizedMessage()); } throw new DateCalcException("An invalid expression was given: " + text); }
@Override public ValueRange range(TemporalField field) { if (field instanceof ChronoField) { if (isSupported(field)) { ChronoField f = (ChronoField) field; switch (f) { case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth()); case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear()); case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, month == 13 ? 1 : 5); case YEAR: case YEAR_OF_ERA: return (prolepticYear <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE)); // TODO } return getChronology().range(f); } throw new UnsupportedTemporalTypeException("Unsupported field: " + field); } return field.rangeRefinedBy(this); }
@Override public long getLong(TemporalField field) { Objects.requireNonNull(field, "field"); Long value = fieldValues.get(field); if (value != null) { return value; } if (date != null && date.isSupported(field)) { return date.getLong(field); } if (time != null && time.isSupported(field)) { return time.getLong(field); } if (field instanceof ChronoField) { throw new UnsupportedTemporalTypeException("Unsupported field: " + field); } return field.getFrom(this); }
@Override public long getLong(TemporalField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case DAY_OF_WEEK: return Math.floorMod(toEpochDay() + 3, 7) + 1; case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((day - 1) % 7) + 1; case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((get(ChronoField.DAY_OF_YEAR) - 1) % 7) + 1; case DAY_OF_MONTH: return day; case DAY_OF_YEAR: return (month - 1) * 30 + day; case EPOCH_DAY: return toEpochDay(); case ALIGNED_WEEK_OF_MONTH: return ((day - 1) / 7) + 1; case ALIGNED_WEEK_OF_YEAR: return ((get(ChronoField.DAY_OF_YEAR) - 1) / 7) + 1; case MONTH_OF_YEAR: return month; case YEAR_OF_ERA: return (prolepticYear >= 1 ? prolepticYear : 1 - prolepticYear); case YEAR: return prolepticYear; case ERA: return (prolepticYear >= 1 ? 1 : 0); } throw new UnsupportedTemporalTypeException("Unsupported field: " + field); } return field.getFrom(this); }
@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); }
@Override public CopticDate plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; switch (f) { case DAYS: return plusDays(amountToAdd); case WEEKS: return plusDays(Math.multiplyExact(amountToAdd, 7)); case MONTHS: return plusMonths(amountToAdd); case YEARS: return plusYears(amountToAdd); case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10)); case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100)); case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000)); } throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } return unit.addTo(this, amountToAdd); }
@Override public long get(TemporalUnit unit) { if (this.unit.equals(unit)) { return value; } if (units.contains(unit)) { return 0; } throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); }
@Override default int get(TemporalField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case INSTANT_SECONDS: throw new UnsupportedTemporalTypeException("Invalid field 'InstantSeconds' for get() method, use getLong() instead"); case OFFSET_SECONDS: return getOffset().getTotalSeconds(); } return toLocalDateTime().get(field); } return Temporal.super.get(field); }
@Override public long get(TemporalUnit unit) { if (unit == ChronoUnit.YEARS) { return years; } else if (unit == ChronoUnit.MONTHS) { return months; } else if (unit == ChronoUnit.DAYS) { return days; } else { throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } }
/** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws UnsupportedTemporalTypeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override default ChronoLocalDate with(TemporalField field, long newValue) { if (field instanceof ChronoField) { throw new UnsupportedTemporalTypeException("Unsupported field: " + field); } return ChronoLocalDateImpl.ensureValid(getChronology(), field.adjustInto(this, newValue)); }
@Override public long getLong(TemporalField field) { if (field instanceof ChronoField) { // same as ISO: // DAY_OF_WEEK, DAY_OF_MONTH, EPOCH_DAY, MONTH_OF_YEAR, PROLEPTIC_MONTH, YEAR // // calendar specific fields // DAY_OF_YEAR, YEAR_OF_ERA, ERA switch ((ChronoField) field) { case ALIGNED_DAY_OF_WEEK_IN_MONTH: case ALIGNED_DAY_OF_WEEK_IN_YEAR: case ALIGNED_WEEK_OF_MONTH: case ALIGNED_WEEK_OF_YEAR: throw new UnsupportedTemporalTypeException("Unsupported field: " + field); case YEAR_OF_ERA: return yearOfEra; case ERA: return era.getValue(); case DAY_OF_YEAR: Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE); jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET); jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth()); return jcal.get(Calendar.DAY_OF_YEAR); } return isoDate.getLong(field); } return field.getFrom(this); }
@Override public long getLong(TemporalField field) { if (this.field.equals(field)) { return value; } throw new UnsupportedTemporalTypeException("Unsupported field: " + field); }
@Override public ValueRange range(TemporalField field) { if (field instanceof ChronoField) { if (isSupported(field)) { return field.range(); } throw new UnsupportedTemporalTypeException("Unsupported field: " + field); } return field.rangeRefinedBy(this); }