private static Date convertDate(Attribute attr, Object value) { if (value instanceof Date) { return (Date) value; } if (value == null) { return null; } // try to convert value Date dateValue; if (value instanceof String) { String paramStrValue = (String) value; try { dateValue = getDateFormat().parse(paramStrValue); } catch (ParseException e) { throw new MolgenisValidationException( new ConstraintViolation( format( "Attribute [%s] value [%s] does not match date format [%s]", attr.getName(), paramStrValue, MolgenisDateFormat.getDateFormat().toPattern()))); } } else { throw new MolgenisValidationException( new ConstraintViolation( format( "Attribute [%s] value is of type [%s] instead of [%s]", attr.getName(), value.getClass().getSimpleName(), String.class.getSimpleName()))); } return dateValue; }
@Test public void convertDate() throws ParseException { AttributeMetaData attr = new DefaultAttributeMetaData("attr").setDataType(MolgenisFieldTypes.DATE); assertEquals( DataConverter.convert("2015-06-04", attr), MolgenisDateFormat.getDateFormat().parse("2015-06-04")); }