private ZonedDateTime zonedDateTime(String value) { return ZONED_DATE_TIME_FORMAT.parse(value, ZonedDateTime::from); }
@Override public Instant parse(String text, Locale locale) throws ParseException { if (text.length() > 0 && Character.isDigit(text.charAt(0))) { // assuming UTC instant a la "2007-12-03T10:15:30.00Z" return Instant.parse(text); } else { // assuming RFC-1123 value a la "Tue, 3 Jun 2008 11:05:30 GMT" return Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(text)); } }
@Override protected Instant deserialize(String key, DeserializationContext ctxt) throws IOException { try { return DateTimeFormatter.ISO_INSTANT.parse(key, Instant::from); } catch (DateTimeException e) { return _handleDateTimeException(ctxt, Instant.class, e, key); } } }
@Override public Instant instantValue() { return value != null ? Instant.from(ISO_INSTANT.parse((CharSequence) value)) : null; }
/** * Like {@link #getInstant(String)} but specifying a default value to return if there is no entry. * * @param key the key to lookup * @param def the default value to use if the entry is not present * @return the value or {@code def} if no entry present */ public Instant getInstant(String key, Instant def) { Objects.requireNonNull(key); Object val = map.get(key); return val != null || map.containsKey(key) ? (val == null ? null : Instant.from(ISO_INSTANT.parse((String) val))) : def; }
@Override public Instant parse(String text, Locale locale) throws ParseException { if (text.length() > 0 && Character.isDigit(text.charAt(0))) { // assuming UTC instant a la "2007-12-03T10:15:30.00Z" return Instant.parse(text); } else { // assuming RFC-1123 value a la "Tue, 3 Jun 2008 11:05:30 GMT" return Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(text)); } }
private boolean meetsPredicate(List<String> fields) { if (!timestampOrdinalPosition.isPresent() || !domain.isPresent()) { return true; } long millis = Instant.from(ISO_FORMATTER.parse(fields.get(timestampOrdinalPosition.getAsInt()))).toEpochMilli(); return domain.get().includesNullableValue(millis); }
@Override public ZonedDateTime fromString(String string) { return ZonedDateTime.from( ZonedDateTimeType.FORMATTER.parse( string ) ); }
@Override public LocalDateTime fromString(String string) { return LocalDateTime.from( LocalDateTimeType.FORMATTER.parse( string ) ); }
/** * Get the instant value with the specified key. * <p> * JSON itself has no notion of a temporal types, this extension complies to the RFC-7493, so this method assumes * there is a String value with the key and it contains an ISO 8601 encoded date and time format * such as "2017-04-03T10:25:41Z", which it decodes if found and returns. * <p> * This method should be used in conjunction with {@link #put(String, java.time.Instant)} * * @param key the key to return the value for * @return the value or null if no value for that key * @throws java.lang.ClassCastException if the value is not a String * @throws java.time.format.DateTimeParseException if the String value is not a legal ISO 8601 encoded value */ public Instant getInstant(String key) { Objects.requireNonNull(key); String encoded = (String) map.get(key); return encoded == null ? null : Instant.from(ISO_INSTANT.parse(encoded)); }
@Override public LocalTime fromString(String string) { return LocalTime.from( LocalTimeType.FORMATTER.parse( string ) ); }
@Override public OffsetTime fromString(String string) { return OffsetTime.from( OffsetTimeType.FORMATTER.parse( string ) ); }
@Override public LocalDate fromString(String string) { return LocalDate.from( LocalDateType.FORMATTER.parse( string ) ); }
@Override public Instant deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { String text = p.getText(); try { return Instant.from(ISO_INSTANT.parse(text)); } catch (DateTimeException e) { throw new InvalidFormatException(p, "Expected an ISO 8601 formatted date time", text, Instant.class); } } }
@Override public long getLong(int field) { if (getType(field).equals(TIMESTAMP)) { return Instant.from(ISO_FORMATTER.parse(getFieldValue(field))).toEpochMilli(); } else { checkFieldType(field, BIGINT, INTEGER); return Long.parseLong(getFieldValue(field)); } }
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; }
@Test public void encodeCustomTypeInstant() { Instant now = Instant.now(); String json = Json.encode(now); assertNotNull(json); // the RFC is one way only Instant decoded = Instant.from(ISO_INSTANT.parse(json.substring(1, json.length() - 1))); assertEquals(now, decoded); }
@Test public void instantConversion() throws Exception { String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100"; MockServerHttpRequest request = MockServerHttpRequest.get("/").header("name", rfc1123val).build(); ServerWebExchange exchange = MockServerWebExchange.from(request); Mono<Object> mono = this.resolver.resolveArgument(this.paramInstant, this.bindingContext, exchange); Object result = mono.block(); assertTrue(result instanceof Instant); assertEquals(Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(rfc1123val)), result); }
@Test public void testInstantDecoding() { Pojo original = new Pojo(); original.instant = Instant.from(ISO_INSTANT.parse("2018-06-20T07:25:38.397Z")); Pojo decoded = Json.decodeValue("{\"instant\":\"2018-06-20T07:25:38.397Z\"}", Pojo.class); assertEquals(original.instant, decoded.instant); }
@Test public void instantConversion() throws Exception { String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100"; servletRequest.addHeader("name", rfc1123val); ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer(); bindingInitializer.setConversionService(new DefaultFormattingConversionService()); Object result = resolver.resolveArgument(paramInstant, null, webRequest, new DefaultDataBinderFactory(bindingInitializer)); assertTrue(result instanceof Instant); assertEquals(Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(rfc1123val)), result); }