Ejemplo n.º 1
0
  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;
  }
Ejemplo n.º 2
0
 @Test
 public void convertDateTime() throws ParseException {
   AttributeMetaData attr =
       new DefaultAttributeMetaData("attr").setDataType(MolgenisFieldTypes.DATETIME);
   assertEquals(
       DataConverter.convert("2015-05-22T11:12:13+0500", attr),
       MolgenisDateFormat.getDateTimeFormat().parse("2015-05-22T11:12:13+0500"));
 }