@Test public void shouldReturnFormattedDateWhenDataValue() throws Exception { WebDataBinder dataBinder = new WebDataBinder(null); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = "2015-10-01 12:00:00"; Date value = dateFormat.parse(dateString); controller.initBinder(dataBinder); CustomDateEditor editor = (CustomDateEditor) dataBinder.findCustomEditor(Date.class, null); editor.setValue(value); String parsedDate = editor.getAsText(); assertThat(dateString, is(parsedDate)); }
/** * Checks that the SDF did not adjust the value based on overflows. * * @param text the text entered by the user * @throws IllegalArgumentException if the parsed text is not the same as the entered text */ @Override public void setAsText(String text) { super.setAsText(text); if (text != null && text.trim().length() > 0) { if (!text.equals(getAsText())) { throw new IllegalArgumentException("Value must be in the format MM/dd/yyyy."); } } }