StringConverter implements Converter { public Object convert(Class destClass, Object srcObj) { Class srcClass = srcObj.getClass(); if (hasDateFormat()) { if (Date.class.isAssignableFrom(srcClass)) { return dateFormatContainer.getDateFormat().format((Date)srcObj); } if (Calendar.class.isAssignableFrom(srcClass)) { return dateFormatContainer.getDateFormat().format(((Calendar)srcObj).getTime()); } else if (TemporalAccessor.class.isAssignableFrom(srcClass)) { return dateFormatContainer.getDateTimeFormatter().format((TemporalAccessor)srcObj); } else { return srcObj.toString(); } } else { return srcObj.toString(); } } StringConverter(DateFormatContainer dateFormatContainer); Object convert(Class destClass, Object srcObj);  }
@Test public void testDateToString_NoDateFormatSpecified() { Date src = new Date(); StringConverter converter = new StringConverter(new DateFormatContainer(null)); String result = (String)converter.convert(String.class, src); assertEquals("incorrect value returned from converter", src.toString(), result); }
@Test public void testCalendarToString_NoDateFormatSpecified() { Calendar src = Calendar.getInstance(); StringConverter converter = new StringConverter(new DateFormatContainer(null)); String result = (String)converter.convert(String.class, src); assertEquals("incorrect value returned from converter", src.toString(), result); }
@Test public void testLocalDateTimeToStringNoFormat() { LocalDateTime src = LocalDateTime.now(); StringConverter converter = new StringConverter(new DateFormatContainer(null)); String result = (String)converter.convert(String.class, src); assertEquals("incorrect value returned from converter", src.toString(), result); }
@Test public void testLocalDateTimeToStringWithFormat() { LocalDateTime src = LocalDateTime.now(); String pattern = "yyyy-MM-dd'T'HH:mm:ss"; String formattedSrc = DateTimeFormatter.ofPattern(pattern).format(src); StringConverter converter = new StringConverter(new DateFormatContainer(pattern)); String result = (String)converter.convert(String.class, src); assertEquals("incorrect value returned from converter", formattedSrc, result); }
@Test public void testZonedDateTimeToStringWithFormat() { ZonedDateTime src = ZonedDateTime.now(); String pattern = "uuuu/MM/dd HH:mm:ss.SSSZZZZZ'['VV']'"; String formattedSrc = DateTimeFormatter.ofPattern(pattern).format(src); StringConverter converter = new StringConverter(new DateFormatContainer(pattern)); String result = (String)converter.convert(String.class, src); assertEquals("incorrect value returned from converter", formattedSrc, result); }