private int parseCount0(@NotNull String name) { try { TemporalAccessor parse = formatter.parse(name); long epochDay = parse.getLong(ChronoField.EPOCH_DAY) * 86400; if (parse.isSupported(ChronoField.SECOND_OF_DAY)) epochDay += parse.getLong(ChronoField.SECOND_OF_DAY); return Maths.toInt32((epochDay - ((epoch) / 1000)) / (length / 1000)); } catch (DateTimeParseException e) { throw new RuntimeException(String.format( "Unable to parse %s using format %s", name, format), e); } }
private static DateTimeFormatter buildDateTimeFormatter(String stringValue) { /* A Java 8 bug causes DateTimeFormatter.withZone to override an explicit time zone in the parsed string, contrary to the documented behavior. So if the string includes a zone do NOT use withZone in building the formatter. */ DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME; ParsePosition pp = new ParsePosition(0); TemporalAccessor accessor = formatter.parseUnresolved(stringValue, pp); if (!accessor.isSupported(ChronoField.OFFSET_SECONDS)) { formatter = formatter.withZone(ZoneId.of("UTC")); } return formatter; }
private static ImmutableListMultimap<Class<?>, ChronoField> buildUnsupported() { ImmutableListMultimap.Builder<Class<?>, ChronoField> builder = ImmutableListMultimap.builder(); for (TemporalAccessor temporalAccessor : TEMPORAL_ACCESSOR_INSTANCES) { for (ChronoField chronoField : ChronoField.values()) { if (!temporalAccessor.isSupported(chronoField)) { builder.put(temporalAccessor.getClass(), chronoField); } } } return builder.build(); }
/** * Best effort parsing of the given value */ public static LocalDateTime parseBest(String value) { TemporalAccessor parsed = getDateTimeFormatter().parse(value); LocalDate datePart; LocalTime timePart; ZoneOffset zoneOffset; long epochDay = 0, nanoSeconds = 0; int offsetSeconds = 0; // get different parsed parts if (parsed.isSupported(ChronoField.EPOCH_DAY)) { epochDay = parsed.getLong(ChronoField.EPOCH_DAY); } if (parsed.isSupported(ChronoField.NANO_OF_DAY)) { nanoSeconds = parsed.getLong(ChronoField.NANO_OF_DAY); } if (parsed.isSupported(ChronoField.OFFSET_SECONDS)) { offsetSeconds = parsed.get(ChronoField.OFFSET_SECONDS); } zoneOffset = ZoneOffset.ofTotalSeconds(offsetSeconds); datePart = LocalDate.ofEpochDay(epochDay); timePart = LocalTime.ofNanoOfDay(nanoSeconds); return OffsetDateTime.of(datePart, timePart, zoneOffset).toLocalDateTime(); }
if (given.isSupported(ChronoField.EPOCH_DAY) && given.isSupported(ChronoField.NANO_OF_DAY)) { if (given.isSupported(ChronoField.EPOCH_DAY)) { dateFromDefault = LocalDate.from(given); } else { final int year; if (given.isSupported(ChronoField.YEAR)) { year = given.get(ChronoField.YEAR); } else if (given.isSupported(ChronoField.YEAR_OF_ERA)) { year = given.get(ChronoField.YEAR_OF_ERA); } else { if (given.isSupported(ChronoField.DAY_OF_YEAR)) { dateFromDefault = LocalDate.ofYearDay(year, given.get(ChronoField.DAY_OF_YEAR)); } else { final int month; if (given.isSupported(ChronoField.MONTH_OF_YEAR)) { month = given.get(ChronoField.MONTH_OF_YEAR); } else { if (given.isSupported(ChronoField.DAY_OF_MONTH)) { dayOfMonth = given.get(ChronoField.DAY_OF_MONTH); } else { if (given.isSupported(ChronoField.NANO_OF_DAY)) { timeFromDefault = LocalTime.from(given); } else { if (given.isSupported(ChronoField.NANO_OF_DAY)) { timeFromDefault = LocalTime.ofNanoOfDay(given.getLong(ChronoField.NANO_OF_DAY));
private static int getOrDefault( final TemporalAccessor accessor, final ChronoField field, final int defaultValue ) { return accessor.isSupported(field) ? accessor.get(field) : defaultValue; } }
private static int getOrDefault( final TemporalAccessor accessor, final ChronoField field, final int defaultValue ) { return accessor.isSupported(field) ? accessor.get(field) : defaultValue; }
private static Optional<Integer> getOrEmpty( final TemporalAccessor accessor, final ChronoField field ) { return accessor.isSupported(field) ? Optional.of(accessor.get(field)) : Optional.empty(); } }
if (accessor.isSupported(ChronoField.INSTANT_SECONDS)) { result = result.with(ChronoField.INSTANT_SECONDS, accessor.getLong(ChronoField.INSTANT_SECONDS)); if (accessor.isSupported(ChronoField.NANO_OF_SECOND)) { result = result.with(ChronoField.NANO_OF_SECOND, accessor.getLong(ChronoField.NANO_OF_SECOND)); if (accessor.isSupported(ChronoField.YEAR)) { result = result.with(ChronoField.YEAR, accessor.getLong(ChronoField.YEAR)); } else if (accessor.isSupported(ChronoField.YEAR_OF_ERA)) { result = result.with(ChronoField.YEAR_OF_ERA, accessor.getLong(ChronoField.YEAR_OF_ERA)); } else if (accessor.isSupported(WeekFields.ISO.weekBasedYear())) { if (accessor.isSupported(WeekFields.ISO.weekOfWeekBasedYear())) { return LocalDate.from(result) .with(WeekFields.ISO.weekBasedYear(), accessor.getLong(WeekFields.ISO.weekBasedYear())) } else if (accessor.isSupported(IsoFields.WEEK_BASED_YEAR)) { if (accessor.isSupported(IsoFields.WEEK_OF_WEEK_BASED_YEAR)) { result = result.with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, accessor.getLong(IsoFields.WEEK_OF_WEEK_BASED_YEAR)); if (accessor.isSupported(ChronoField.MONTH_OF_YEAR)) { result = result.with(ChronoField.MONTH_OF_YEAR, accessor.getLong(ChronoField.MONTH_OF_YEAR)); if (accessor.isSupported(ChronoField.DAY_OF_MONTH)) { result = result.with(ChronoField.DAY_OF_MONTH, accessor.getLong(ChronoField.DAY_OF_MONTH)); if (accessor.isSupported(ChronoField.HOUR_OF_DAY)) { result = result.with(ChronoField.HOUR_OF_DAY, accessor.getLong(ChronoField.HOUR_OF_DAY)); if (accessor.isSupported(ChronoField.MINUTE_OF_HOUR)) {
@Override public boolean isSupportedBy(TemporalAccessor temporal) { return temporal.isSupported(DAY_OF_YEAR) && temporal.isSupported(MONTH_OF_YEAR) && temporal.isSupported(YEAR) && isIso(temporal); } @Override
protected boolean hasYear(TemporalAccessor temporalAccessor){ if (temporalAccessor.isSupported(ChronoField.YEAR_OF_ERA)){ return true; } return temporalAccessor.isSupported(ChronoField.YEAR); }
@Override public boolean isSupportedBy(TemporalAccessor temporal) { return temporal.isSupported(EPOCH_DAY) && isIso(temporal); } @Override
protected int getChronoField(TemporalAccessor temporalAccessor, ChronoField chronoField){ if (temporalAccessor.isSupported(chronoField)){ return temporalAccessor.get(chronoField); } return 0; }
@Override public ValueRange rangeRefinedBy(TemporalAccessor temporal) { if (temporal.isSupported(this) == false) { throw new UnsupportedTemporalTypeException("Unsupported field: WeekOfWeekBasedYear"); } return getWeekRange(LocalDate.from(temporal)); } @Override
@Override public ValueRange rangeRefinedBy(TemporalAccessor temporal) { if (!temporal.isSupported(this)) { throw new DateTimeException("Unsupported field: " + this); } return range(); }
@Override public ValueRange rangeRefinedBy(TemporalAccessor temporal) { if (!temporal.isSupported(this)) { throw new DateTimeException("Unsupported field: " + this); } return range(); }
@Override public LocalDate queryFrom(TemporalAccessor temporal) { if (temporal.isSupported(EPOCH_DAY)) { return LocalDate.ofEpochDay(temporal.getLong(EPOCH_DAY)); } return null; } };
@Override public LocalTime queryFrom(TemporalAccessor temporal) { if (temporal.isSupported(NANO_OF_DAY)) { return LocalTime.ofNanoOfDay(temporal.getLong(NANO_OF_DAY)); } return null; } };
@Override public long getFrom(TemporalAccessor temporal) { if (temporal.isSupported(this) == false) { throw new UnsupportedTemporalTypeException("Unsupported field: QuarterOfYear"); } long moy = temporal.getLong(MONTH_OF_YEAR); return ((moy + 2) / 3); } @SuppressWarnings("unchecked")
private Date convertToDate(TemporalAccessor t) { if(!t.isSupported(ChronoField.INSTANT_SECONDS)) { t = LocalDateTime.of(LocalDate.from(t), LocalTime.MIN).atOffset(ZoneOffset.UTC); } return new Date((t.getLong(ChronoField.INSTANT_SECONDS) * 1000L) + t.getLong(ChronoField.MILLI_OF_SECOND)); } };