@Override public void setAsText(String text) throws IllegalArgumentException { setValue(ZoneId.of(text)); }
@Override protected Object deserialize(String key, DeserializationContext ctxt) throws IOException { try { return ZoneId.of(key); } catch (DateTimeException e) { return _handleDateTimeException(ctxt, ZoneId.class, e, key); } } }
@JsonValue @Override public String toString() { return Instant.ofEpochMilli(millisUtc).atZone(ZoneId.of(timeZoneKey.getId())).format(formatter); } }
@JsonValue @Override public String toString() { if (isLegacyTimestamp()) { return Instant.ofEpochMilli(millis).atZone(ZoneId.of(sessionTimeZoneKey.get().getId())).format(formatter); } else { return Instant.ofEpochMilli(millis).atZone(ZoneOffset.UTC).format(formatter); } }
@JsonValue @Override public String toString() { return Instant.ofEpochMilli(millisUtc).atZone(ZoneId.of(timeZoneKey.getId())).format(formatter); } }
@JsonValue @Override public String toString() { if (isLegacyTimestamp()) { return Instant.ofEpochMilli(millis).atZone(ZoneId.of(sessionTimeZoneKey.get().getId())).format(formatter); } else { return Instant.ofEpochMilli(millis).atZone(ZoneOffset.UTC).format(formatter); } }
@Test public void lastModified() { HttpHeaders headers = new HttpHeaders(); ZonedDateTime lastModified = ZonedDateTime.of(2018, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")); headers.setLastModified(lastModified.toInstant().toEpochMilli()); HeaderAssertions assertions = headerAssertions(headers); assertions.lastModified(lastModified.toInstant().toEpochMilli()); try { assertions.lastModified(lastModified.toInstant().toEpochMilli() + 1); fail("Wrong value expected"); } catch (AssertionError error) { // Expected } }
@Test public void convertObjectToStringWithJavaTimeOfMethodPresent() { assertTrue(conversionService.convert(ZoneId.of("GMT+1"), String.class).startsWith("GMT+")); }
@Test public void expires() { HttpHeaders headers = new HttpHeaders(); ZonedDateTime expires = ZonedDateTime.of(2018, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")); headers.setExpires(expires); HeaderAssertions assertions = headerAssertions(headers); assertions.expires(expires.toInstant().toEpochMilli()); try { assertions.expires(expires.toInstant().toEpochMilli() + 1); fail("Wrong value expected"); } catch (AssertionError error) { // Expected } }
@Test // SPR-17330 public void matchDateFormattedWithHttpHeaders() throws Exception { long epochMilli = ZonedDateTime.of(2018, 10, 5, 0, 0, 0, 0, ZoneId.of("GMT")).toInstant().toEpochMilli(); HttpHeaders headers = new HttpHeaders(); headers.setDate("myDate", epochMilli); this.response.setHeader("d", headers.getFirst("myDate")); this.matchers.dateValue("d", epochMilli).match(this.mvcResult); }
@Test public void expiresZonedDateTime() { ZonedDateTime zonedDateTime = ZonedDateTime.of(2008, 12, 18, 10, 20, 0, 0, ZoneId.of("GMT")); headers.setExpires(zonedDateTime); assertEquals("Invalid Expires header", zonedDateTime.toInstant().toEpochMilli(), headers.getExpires()); assertEquals("Invalid Expires header", "Thu, 18 Dec 2008 10:20:00 GMT", headers.getFirst("expires")); }
@Test public void convertObjectToObjectWithJavaTimeOfMethod() { assertEquals(ZoneId.of("GMT+1"), conversionService.convert("GMT+1", ZoneId.class)); }
@Test public void getValueAsText() { editor.setValue(ZoneId.of("America/New_York")); assertEquals("The text version is not correct.", "America/New_York", editor.getAsText()); }
@Test public void firstZonedDateTime() { ZonedDateTime date = ZonedDateTime.of(2017, 6, 22, 22, 22, 0, 0, ZoneId.of("GMT")); headers.setZonedDateTime(HttpHeaders.DATE, date); assertThat(headers.getFirst(HttpHeaders.DATE), is("Thu, 22 Jun 2017 22:22:00 GMT")); assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date)); headers.clear(); ZonedDateTime otherDate = ZonedDateTime.of(2010, 12, 18, 10, 20, 0, 0, ZoneId.of("GMT")); headers.add(HttpHeaders.DATE, RFC_1123_DATE_TIME.format(date)); headers.add(HttpHeaders.DATE, RFC_1123_DATE_TIME.format(otherDate)); assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date)); // obsolete RFC 850 format headers.clear(); headers.set(HttpHeaders.DATE, "Thursday, 22-Jun-17 22:22:00 GMT"); assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date)); // ANSI C's asctime() format headers.clear(); headers.set(HttpHeaders.DATE, "Thu Jun 22 22:22:00 2017"); assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date)); }
@Test public void resolveCustomizedAndTimeZoneLocale() { TimeZone timeZone = TimeZone.getTimeZone(ZoneId.of("UTC")); FixedLocaleContextResolver resolver = new FixedLocaleContextResolver(FRANCE, timeZone); TimeZoneAwareLocaleContext context = (TimeZoneAwareLocaleContext) resolver.resolveLocaleContext(exchange()); assertEquals(FRANCE, context.getLocale()); assertEquals(timeZone, context.getTimeZone()); }
@Test public void americaLosAngeles() { editor.setAsText("America/Los_Angeles"); ZoneId zoneId = (ZoneId) editor.getValue(); assertNotNull("The zone ID should not be null.", zoneId); assertEquals("The zone ID is not correct.", ZoneId.of("America/Los_Angeles"), zoneId); assertEquals("The text version is not correct.", "America/Los_Angeles", editor.getAsText()); }
@Test public void americaChicago() { editor.setAsText("America/Chicago"); ZoneId zoneId = (ZoneId) editor.getValue(); assertNotNull("The zone ID should not be null.", zoneId); assertEquals("The zone ID is not correct.", ZoneId.of("America/Chicago"), zoneId); assertEquals("The text version is not correct.", "America/Chicago", editor.getAsText()); }
private void setup(DateTimeFormatterRegistrar registrar) { conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); registrar.registerFormatters(conversionService); DateTimeBean bean = new DateTimeBean(); bean.getChildren().add(new DateTimeBean()); binder = new DataBinder(bean); binder.setConversionService(conversionService); LocaleContextHolder.setLocale(Locale.US); DateTimeContext context = new DateTimeContext(); context.setTimeZone(ZoneId.of("-05:00")); DateTimeContextHolder.setDateTimeContext(context); }