ISO8601DateSerde implements Serde<String, Date> { @Override public Date deserialize(String val) { Date date; try { date = df.parse(val); } catch (java.text.ParseException e) { throw new IllegalArgumentException("Date strings must be formated as " + df.getPattern()); } if (ClassUtils.isAssignable(targetType, java.sql.Date.class)) { return new java.sql.Date(date.getTime()); } else if (ClassUtils.isAssignable(targetType, java.sql.Timestamp.class)) { return new java.sql.Timestamp(date.getTime()); } else if (ClassUtils.isAssignable(targetType, java.sql.Time.class)) { return new java.sql.Time(date.getTime()); } else { return date; } } ISO8601DateSerde(SimpleDateFormat df);  ISO8601DateSerde(SimpleDateFormat df, Class<? extends Date> targetType);  ISO8601DateSerde(String formatString, TimeZone tz);  ISO8601DateSerde(String formatString, TimeZone tz, Class<? extends Date> targetType);  ISO8601DateSerde(); @Override Date deserialize(String val); @Override String serialize(Date val);  }
@Test public void testDateDeserialization() throws Exception { ISO8601DateSerde serde = new ISO8601DateSerde(); assertEquals(serde.deserialize("1970-01-01T00:00Z"), new Date(0)); }
@Test public void testSQLDateDeserialization() throws Exception { ISO8601DateSerde serde = new ISO8601DateSerde("yyyy-MM-dd'T'HH:mm'Z'", TimeZone.getTimeZone("UTC"), java.sql.Date.class); assertEquals(serde.deserialize("1970-01-01T00:00Z"), new java.sql.Date(0)); }
@Test public void testSQLTimeDeserialization() throws Exception { ISO8601DateSerde serde = new ISO8601DateSerde("yyyy-MM-dd'T'HH:mm'Z'", TimeZone.getTimeZone("UTC"), java.sql.Time.class); assertEquals(serde.deserialize("1970-01-01T00:00Z"), new java.sql.Time(0)); }
@Test public void testSQLTimestampDeserialization() throws Exception { ISO8601DateSerde serde = new ISO8601DateSerde("yyyy-MM-dd", TimeZone.getTimeZone("UTC"), java.sql.Timestamp.class); java.sql.Timestamp expected = new java.sql.Timestamp(0); java.sql.Timestamp actual = (java.sql.Timestamp) serde.deserialize("1970-01-01"); assertEquals(expected, actual); }
@Test public void testInvalidDateDeserialization() { ISO8601DateSerde serde = new ISO8601DateSerde(); assertThrows(IllegalArgumentException.class, () -> serde.deserialize("1")); }
@Test public void testInvalidDateDeserializationMessage() { ISO8601DateSerde serde = new ISO8601DateSerde("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC")); IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> serde.deserialize("2019-01-01T00:00Z")); assertEquals("Date strings must be formated as yyyy-MM-dd'T'HH:mm:ss'Z'", e.getMessage()); }