ConstraintValueParser { public static Object parse(String value, Class<?> type) throws ParseValueException { Object parsedValue = null; if (value == null || value.length() == 0) { throw new ParseValueException("No input given, please supply a valid expression"); } if (Date.class.equals(type)) { try { parsedValue = ISO_DATE_FORMAT.parse(value); } catch (ParseException e) { throw new ParseValueException(value + " is not a valid date - example: " + ISO_DATE_FORMAT.format(new Date())); } } else if (String.class.equals(type)) { if (value.length() == 0) { throw new ParseValueException("Please supply a valid expression."); } else { parsedValue = value.trim(); } } else { try { parsedValue = TypeUtil.stringToObject(type, value); if (parsedValue instanceof String) { parsedValue = ((String) parsedValue).trim(); } } catch (NumberFormatException e) { throw new ParseValueException(value + " is not a valid number."); } } return parsedValue; } private  ConstraintValueParser(); static Object parse(String value, Class<?> type); static String format(String value); static final DateFormat ISO_DATE_FORMAT; }
@Test public static void testParsing() throws Exception { String aDate=date.toString(); Object convertedDate = ConstraintValueParser.parse(aDate, Date.class); assertEquals("qq", aDate, convertedDate); Object converted = ConstraintValueParser.parse(DATE_STRING, String.class); assertEquals("qq", DATE_STRING, converted); }