Refine search
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; }
return GregorianCalendar.from(ldt.atZone(ZoneId.systemDefault())); } catch (final DateTimeParseException e) { return GregorianCalendar.from(instant.atZone(ZoneId.systemDefault())); } catch (final DateTimeParseException e) { final OffsetDateTime odt = BASIC_DATE_TIME.parse(str, OffsetDateTime::from); return GregorianCalendar.from(odt.atZoneSameInstant(ZoneId.systemDefault())); } catch (final DateTimeParseException e) { final OffsetDateTime odt = STD_ORDINAL_DATE_TIME.parse(str, OffsetDateTime::from); return GregorianCalendar.from(odt.atZoneSameInstant(ZoneId.systemDefault())); } catch (final DateTimeParseException e) { final OffsetDateTime odt = BASIC_ORDINAL_DATE_TIME.parse(str, OffsetDateTime::from); return GregorianCalendar.from(odt.atZoneSameInstant(ZoneId.systemDefault())); } catch (final DateTimeParseException e) { final LocalTime lt = STD_HOUR.parse(str, LocalTime::from); return GregorianCalendar.from(lt.atDate(LocalDate.ofEpochDay(0)).atZone(ZoneId.systemDefault())); } catch (final DateTimeParseException e) { final TemporalAccessor ta = STD_YEAR_WEEK.withLocale(Locale.getDefault()).parse(str); final int y = ta.get(WeekFields.ISO.weekBasedYear()); final int w = ta.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR); return GregorianCalendar.from(LocalDateTime .from(ta)
for (java.time.format.DateTimeFormatter formatter : DATE_TIME_FORMATS) { try { TemporalAccessor datetime = formatter.parse(value); ZoneId parsedTimezone = datetime.query(TemporalQueries.zoneId()); ZoneOffset parsedOffset = datetime.query(TemporalQueries.offset()); if (parsedTimezone != null) { return Optional.of(java.time.Instant.from(datetime)); } else if (parsedOffset != null) { try { return Optional.of(java.time.Instant.ofEpochSecond(datetime.getLong(ChronoField.INSTANT_SECONDS))); } catch (UnsupportedTemporalTypeException e) { return Optional.of(java.time.LocalDate.of( datetime.get(ChronoField.YEAR), datetime.get(ChronoField.MONTH_OF_YEAR), datetime.get(ChronoField.DAY_OF_MONTH) ).atStartOfDay().toInstant(parsedOffset)); if (timezone.isPresent()) { java.time.Instant reference = java.time.LocalDate.of( datetime.get(ChronoField.YEAR), datetime.get(ChronoField.MONTH_OF_YEAR), datetime.get(ChronoField.DAY_OF_MONTH) ).atStartOfDay().toInstant(ZoneOffset.UTC); ZoneOffset currentOffsetForMyZone = timezone.get().getRules().getOffset(reference); try { return Optional.of(java.time.LocalDateTime.of( datetime.get(ChronoField.YEAR), datetime.get(ChronoField.MONTH_OF_YEAR), datetime.get(ChronoField.DAY_OF_MONTH),
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); } }
final ZoneId givenZoneId = given.query(TemporalQueries.zoneId()); if (givenZoneId != null) { throw new TimestampParseException("Non-offset zone IDs are unaccepted in 'java:' formats: " + this.pattern); final ZoneOffset givenZoneOffset = given.query(TemporalQueries.offset()); 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 { year = 1970; 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 { month = 1; if (given.isSupported(ChronoField.DAY_OF_MONTH)) { dayOfMonth = given.get(ChronoField.DAY_OF_MONTH);
@Override protected Object customTransform(Object value) { if (value instanceof Instant) { value = LocalDateTime.ofInstant((Instant) value, ZoneId.systemDefault()); } if (value instanceof TemporalAccessor) { if (((TemporalAccessor) value).isSupported(ChronoField.HOUR_OF_DAY)) { return DateTimeFormatter.ISO_LOCAL_DATE_TIME.format((TemporalAccessor) value); } else { return DateTimeFormatter.ISO_LOCAL_DATE.format((TemporalAccessor) value); } } return value; }
private static Calendar fromTemporal(TemporalAccessor temporal) { /* * The code below was adapted from ZonedDateTime.from * to also support a zoned date without time, and a local date or date/time. * This is to avoid breaking compatibility with applications developed against * Hibernate Search 5.10.2 and below, which were relying on * javax.xml.bind.DatatypeConverter.parseDateTime for parsing, * and were thus very lenient. */ LocalDate date = LocalDate.from( temporal ); LocalTime time = temporal.query( TemporalQueries.localTime() ); if ( time == null ) { time = LocalTime.of( 0, 0, 0 ); } ZoneId zoneId = temporal.query( TemporalQueries.zone() ); if ( zoneId == null ) { zoneId = TimeZone.getDefault().toZoneId(); } return GregorianCalendar.from( ZonedDateTime.of( date, time, zoneId ) ); } }
DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("MMM ppd HH:mm:ss").withZone(deviceTimeZone); TemporalAccessor inputDate = inputFormat.parse(logTimestamp); int inputMonth = inputDate.get(MONTH_OF_YEAR); int inputDay = inputDate.get(DAY_OF_MONTH); int inputHour = inputDate.get(HOUR_OF_DAY); int inputMinute = inputDate.get(MINUTE_OF_HOUR); int inputSecond = inputDate.get(SECOND_OF_MINUTE);
@Override protected Instant convert2Target(DateTimeFormatter formatter, String obj) { TemporalAccessor temporal = formatter.parse(obj); if (temporal instanceof Instant) { return (Instant) temporal; } Objects.requireNonNull(temporal, "temporal"); try { long instantSecs = temporal.getLong(INSTANT_SECONDS); int nanoOfSecond = temporal.get(NANO_OF_SECOND); return Instant.ofEpochSecond(instantSecs, nanoOfSecond); } catch (DateTimeException e) { // which means it may be a local date time or local date only string ZonedDateTime zdt; try { LocalDateTime ldt = LocalDateTime.from(temporal); zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault()); } catch (DateTimeException ex) { // a local date only string zdt = ZonedDateTime.of(LocalDate.from(temporal).atStartOfDay(), ZoneId.systemDefault()); } return zdt.toInstant(); } } }
static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm"); TemporalAccessor parsed = TIME_FORMATTER.parse("24:00"); LocalTime lt = LocalTime.from(parsed); Period excessDays = parsed.query(DateTimeFormatter.parsedExcessDays()); System.out.println(lt + " + " + excessDays); // 00:00 + P1D
/** * Creates a {@link ZonedDateTime} instance based on the given pattern in ISO 8601 format, compatible with the Joda date-time * library. * <p> * Note that there is no direct correspondence between the JODA-style dates and the new JDK 8 date, especially * when it comes to handling milliseconds. * </p> * * @param iso8601 a {@link String} representing a date and/or time pattern, may not be null * @return a {@link ZonedDateTime} instance, never {@code null} * * @throws java.time.format.DateTimeParseException if the given pattern cannot be parsed */ public static ZonedDateTime jodaParse( String iso8601 ) throws DateTimeParseException { CheckArg.isNotNull(iso8601, "iso8601"); TemporalAccessor parse = JODA_ISO8601_FORMATTER.parse(iso8601); LocalDate localDate = LocalDate.from(parse); LocalTime localTime = parse.isSupported(ChronoField.HOUR_OF_DAY) ? LocalTime.from(parse) : LocalTime.MIDNIGHT; ZoneId zoneId = parse.isSupported(ChronoField.OFFSET_SECONDS) ? ZoneId.from(parse) : UTC; return ZonedDateTime.of(localDate, localTime, zoneId); }
/** * 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 (columnType == TIMESTAMP) { TemporalAccessor parseResult = ISO_DATE_TIME.parse(textValue); return TimeUnit.DAYS.toMillis(parseResult.getLong(EPOCH_DAY)) + parseResult.getLong(MILLI_OF_DAY); TemporalAccessor parseResult = ISO_OFFSET_DATE_TIME.parse(textValue); return packDateTimeWithZone(parseResult.getLong(INSTANT_SECONDS) * 1000 + parseResult.getLong(MILLI_OF_SECOND), getTimeZoneKey(ZoneId.from(parseResult).getId())); return ISO_TIME.parse(textValue).getLong(MILLI_OF_DAY); TemporalAccessor parseResult = ISO_OFFSET_TIME.parse(textValue); return packDateTimeWithZone(parseResult.get(MILLI_OF_DAY), getTimeZoneKey(ZoneId.from(parseResult).getId())); return ISO_DATE.parse(textValue).getLong(EPOCH_DAY);
public static TimeExpression parse(final Context c, final String input) { final ImmutableList.Builder<Throwable> errors = ImmutableList.builder(); for (final DateTimeFormatter f : FORMATTERS) { final TemporalAccessor accessor; try { accessor = f.parse(input); } catch (final DateTimeException e) { errors.add(e); continue; } final int hours = accessor.get(ChronoField.HOUR_OF_DAY); final int minutes = accessor.get(ChronoField.MINUTE_OF_HOUR); final int seconds = getOrDefault(accessor, ChronoField.SECOND_OF_MINUTE, 0); final int milliSeconds = getOrDefault(accessor, ChronoField.MILLI_OF_SECOND, 0); return new TimeExpression(c, hours, minutes, seconds, milliSeconds); } final IllegalArgumentException e = new IllegalArgumentException("Invalid instant: " + input); errors.build().forEach(e::addSuppressed); throw e; }
@Override protected Instant parse(String input) throws Exception { if (CharMatcher.DIGIT.matchesAllOf(input)) { // Assume millis, but check for nanos long millis = Long.parseLong(input); if (millis >= 253402300800000L) { // +10000-01-01T00:00:00Z millis = TimeUnit.NANOSECONDS.toMillis(millis); } return Instant.ofEpochMilli(millis); } try { return ZonedDateTime.parse(input).toInstant(); } catch (DateTimeParseException e) { TemporalAccessor ta = RFC_822_FORMAT.parse(input); LocalDateTime dateTime = LocalDateTime.from(ta); return ZonedDateTime.of(dateTime, ta.isSupported(ChronoField.OFFSET_SECONDS) ? ZoneId.from(ta) : ZoneOffset.UTC).toInstant(); } } }
static ZoneId parseZoneName( String zoneName ) { ZoneId parsedName; try { parsedName = ZONE_NAME_PARSER.parse( zoneName.replace( ' ', '_' ) ).query( TemporalQueries.zoneId() ); } catch ( DateTimeParseException e ) { throw new TemporalParseException( "Invalid value for TimeZone: " + e.getMessage(), e.getParsedString(), e.getErrorIndex(), e ); } return parsedName; }
@Override protected XMLGregorianCalendar parseWithFormatter(String jsonValue, DateTimeFormatter formatter) { final TemporalAccessor parsed = formatter.parse(jsonValue); LocalTime time = parsed.query(TemporalQueries.localTime()); ZoneId zone = parsed.query(TemporalQueries.zone()); if (zone == null) { zone = UTC; } if (time == null) { time = ZERO_LOCAL_TIME; } ZonedDateTime result = LocalDate.from(parsed).atTime(time).atZone(zone); return datatypeFactory.newXMLGregorianCalendar(GregorianCalendar.from(result)); } }
public Long toLong(File file) { final Long cachedValue = filenameToTimestampCache.get(file); if (cachedValue != null) { return cachedValue; } final TemporalAccessor parse = formatter.parse(fileToName.apply(file)); final long value; if (length == ONE_DAY_IN_MILLIS) { value = parse.getLong(ChronoField.EPOCH_DAY); } else { value = Instant.from(parse).toEpochMilli() / length; } if (filenameToTimestampCache.size() >= MAX_TIMESTAMP_CACHE_SIZE) { filenameToTimestampCache.clear(); } filenameToTimestampCache.put(file, value); return value; }
private static ZonedDateTime parseZonedDateTime(final String text, final DateTimeFormatter formatter, final ZoneId defaultZone){ TemporalAccessor parse = formatter.parse(text); ZoneId zone = parse.query(TemporalQueries.zone()); if (Objects.isNull(zone)) { zone = defaultZone; } int year = parse.isSupported(YEAR) ? parse.get(YEAR) : 0; int month = parse.isSupported(MONTH_OF_YEAR) ? parse.get(MONTH_OF_YEAR) : 0; int day = parse.isSupported(DAY_OF_MONTH) ? parse.get(DAY_OF_MONTH) : 0; int hour = parse.isSupported(HOUR_OF_DAY) ? parse.get(HOUR_OF_DAY) : 0; int minute = parse.isSupported(MINUTE_OF_HOUR) ? parse.get(MINUTE_OF_HOUR) : 0; int second = parse.isSupported(SECOND_OF_MINUTE) ? parse.get(SECOND_OF_MINUTE) : 0; int millisecond = parse.isSupported(MILLI_OF_SECOND) ? parse.get(MILLI_OF_SECOND) : 0; return ZonedDateTime.of(year, month, day, hour, minute, second, millisecond, zone); }
TemporalAccessor parsedInfo = parseDf.parse(valStr); if(parsedInfo.isSupported(ChronoField.YEAR)) { ld = LocalDate.from(parsedInfo); } else { lt = LocalTime.from(parsedInfo);