@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 public Instant instantValue() { return value != null ? Instant.from(ISO_INSTANT.parse((CharSequence) value)) : null; }
@Override public Instant fromString(String string) { return Instant.from( DateTimeFormatter.ISO_INSTANT.parse( string ) ); }
/** * 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)); } }
/** * 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 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); } } }
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); }
public static Date toDate(String time, String pattern) { LocalDate formatted = LocalDate.parse(time, DateTimeFormatter.ofPattern(pattern)); return Date.from(Instant.from(formatted.atStartOfDay(ZoneId.systemDefault()))); }
public static Date toDate(String time, String pattern) { LocalDate formatted = LocalDate.parse(time, DateTimeFormatter.ofPattern(pattern)); return Date.from(Instant.from(formatted.atStartOfDay(ZoneId.systemDefault()))); }
@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)); } }
/** * This is the same as IDLE but instead of a relative amount of milliseconds, it sets the idle time to a specific unix time * (in milliseconds). This is useful in order to rewrite the AOF file generating XCLAIM commands. * * @param timestamp idle time. * @return {@code this}. */ public XClaimArgs time(TemporalAccessor timestamp) { LettuceAssert.notNull(timestamp, "Timestamp must not be null"); return time(Instant.from(timestamp).toEpochMilli()); }
@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); }
private void setUpIdToken(Map<String, Object> claims) { Instant issuedAt = Instant.now(); Instant expiresAt = Instant.from(issuedAt).plusSeconds(3600); this.setUpIdToken(claims, issuedAt, expiresAt); }
@Before public void setup() { this.manager = new JwtReactiveAuthenticationManager(this.jwtDecoder); Map<String, Object> claims = new HashMap<>(); claims.put("scope", "message:read message:write"); Instant issuedAt = Instant.now(); Instant expiresAt = Instant.from(issuedAt).plusSeconds(3600); this.jwt = new Jwt("jwt", issuedAt, expiresAt, claims, claims); }
@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"; 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(expected = IllegalArgumentException.class) public void constructorWhenIssuedAtAfterExpiresAtThenThrowIllegalArgumentException() { new OAuth2AccessToken(TOKEN_TYPE, TOKEN_VALUE, Instant.from(EXPIRES_AT).plusSeconds(1), EXPIRES_AT); }
@Test(expected = IllegalArgumentException.class) public void constructorWhenExpiresAtBeforeIssuedAtThenThrowIllegalArgumentException() { new OAuth2AccessToken(TOKEN_TYPE, TOKEN_VALUE, ISSUED_AT, Instant.from(ISSUED_AT).minusSeconds(1)); }
@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); }