protected boolean isValidDate(Chronology chrono, LocalDate date) { try { if (date != null) { chrono.date(date); } return true; } catch (DateTimeException ex) { return false; } } }
@Override public String toString(final Object obj) { return obj == null ? null : ((Chronology)obj).getId(); } }
/** * Constructs a HijrahDateConverter instance. */ public HijrahDateConverter() { hijrahChronologies = new HashSet<>(); final Set<Chronology> chronologies = Chronology.getAvailableChronologies(); for (final Chronology chronology : chronologies) { if (chronology instanceof HijrahChronology) { hijrahChronologies.add(chronology); } } }
@Override long getValue(DateTimePrintContext context, long value) { long absValue = Math.abs(value); int baseValue = this.baseValue; if (baseDate != null) { Chronology chrono = Chronology.from(context.getTemporal()); baseValue = chrono.date(baseDate).get(field); } if (value >= baseValue && value < baseValue + EXCEED_POINTS[minWidth]) { return absValue % EXCEED_POINTS[minWidth]; } return absValue % EXCEED_POINTS[maxWidth]; }
/** * Casts the {@code Temporal} to {@code ChronoLocalDate} with the same chronology. * * @param temporal a date-time to cast, not null * @return the date-time checked and cast to {@code ChronoLocalDate}, not null * @throws ClassCastException if the date-time cannot be cast to ChronoLocalDate * or the chronology is not equal this Chrono */ <D extends ChronoLocalDate> D ensureChronoLocalDate(Temporal temporal) { @SuppressWarnings("unchecked") D other = (D) temporal; if (this.equals(other.getChronology()) == false) { throw new ClassCastException("Chrono mismatch, expected: " + getId() + ", actual: " + other.getChronology().getId()); } return other; }
@Override public int parse(DateTimeParseContext context, CharSequence text, int position) { // simple looping parser to find the chronology if (position < 0 || position > text.length()) { throw new IndexOutOfBoundsException(); } Set<Chronology> chronos = Chronology.getAvailableChronologies(); Chronology bestMatch = null; int matchLen = -1; for (Chronology chrono : chronos) { String id = chrono.getId(); int idLen = id.length(); if (idLen > matchLen && context.subSequenceEquals(text, position, id, 0, idLen)) { bestMatch = chrono; matchLen = idLen; } } if (bestMatch == null) { return ~position; } context.setParsed(bestMatch); return position + matchLen; } }
private static void init() { if (CHRONOS_BY_ID.isEmpty()) { register(IsoChronology.INSTANCE); register(ThaiBuddhistChronology.INSTANCE); register(MinguoChronology.INSTANCE); register(JapaneseChronology.INSTANCE); register(HijrahChronology.INSTANCE); CHRONOS_BY_ID.putIfAbsent("Hijrah", HijrahChronology.INSTANCE); CHRONOS_BY_TYPE.putIfAbsent("islamic", HijrahChronology.INSTANCE); ServiceLoader<Chronology> loader = ServiceLoader.load(Chronology.class, Chronology.class.getClassLoader()); for (Chronology chrono : loader) { CHRONOS_BY_ID.putIfAbsent(chrono.getId(), chrono); String type = chrono.getCalendarType(); if (type != null) { CHRONOS_BY_TYPE.putIfAbsent(type, chrono); } } } }
/** * Obtains a local date in this chronology from the era, year-of-era, * month-of-year and day-of-month fields. * * @param era the era of the correct type for the chronology, not null * @param yearOfEra the chronology year-of-era * @param month the chronology month-of-year * @param dayOfMonth the chronology day-of-month * @return the local date in this chronology, not null * @throws DateTimeException if unable to create the date * @throws ClassCastException if the {@code era} is not of the correct type for the chronology */ public ChronoLocalDate date(Era era, int yearOfEra, int month, int dayOfMonth) { return date(prolepticYear(era, yearOfEra), month, dayOfMonth); }
private static void register(Chronology chrono) { CHRONOS_BY_ID.putIfAbsent(chrono.getId(), chrono); String type = chrono.getCalendarType(); if (type != null) { CHRONOS_BY_TYPE.putIfAbsent(type, chrono); } }
@Override public Chronology fromString(final String str) { if (str == null) { return null; } try { return Chronology.of(str); } catch (final DateTimeException e) { final ConversionException exception = new ConversionException("Cannot parse value as chronology", e); exception.add("value", str); throw exception; } }
@Override public Chronology queryFrom(TemporalAccessor temporal) { return Chronology.from(temporal); } };
return chrono.zonedDateTime(Instant.from(temporal), overrideZone); if (overrideChrono != null) { if (temporal.isSupported(EPOCH_DAY)) { effectiveDate = effectiveChrono.date(temporal); } else {
@SuppressWarnings("unchecked") @Override public ChronoDateImpl<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(Jdk8Methods.safeMultiply(amountToAdd, 7)); case MONTHS: return plusMonths(amountToAdd); 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: throw new DateTimeException("Unable to add era, standard calendar system only has one era"); // case FOREVER: return (period == 0 ? this : (period > 0 ? LocalDate.MAX_DATE : LocalDate.MIN_DATE)); } throw new DateTimeException(unit + " not valid for chronology " + getChronology().getId()); } return (ChronoDateImpl<D>) getChronology().ensureChronoLocalDate(unit.addTo(this, amountToAdd)); }
import java.time.*; import java.time.chrono.*; import java.time.temporal.*; public class Test { public static void main(String[] args) { Chronology hijrah = HijrahChronology.INSTANCE; ValueRange range = hijrah.range(ChronoField.YEAR); System.out.println(range.getMinimum()); System.out.println(range.getMaximum()); } }
@Override public TemporalAccessor resolve(Map<TemporalField, Long> fieldValues, TemporalAccessor partialTemporal, ResolverStyle resolverStyle) { long value = fieldValues.remove(this); Chronology chrono = Chronology.from(partialTemporal); return chrono.dateEpochDay(Jdk8Methods.safeSubtract(value, offset)); }
cmp = value.getChronology().compareTo( that.value.getChronology() );
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof ChronoPeriodImpl) { ChronoPeriodImpl other = (ChronoPeriodImpl) obj; return years == other.years && months == other.months && days == other.days && chronology.equals(other.chronology); } return false; }
AbstractDate resolveEpochDay(long epochDay) { return (AbstractDate) getChronology().dateEpochDay(epochDay); }
@Override public ChronoLocalDate resolve( Map<TemporalField, Long> fieldValues, TemporalAccessor partialTemporal, ResolverStyle resolverStyle) { long value = fieldValues.remove(this); LocalDate date; if (resolverStyle == ResolverStyle.LENIENT) { int year = Math.toIntExact(value / 10000); int moy = (int) ((value % 10000) / 100); long dom = value % 100; date = LocalDate.of(year, 1, 1).plusMonths(moy - 1).plusDays(dom - 1); } else { date = toDate(value); } Chronology chrono = Chronology.from(partialTemporal); return chrono.date(date); }