Esempio n. 1
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"));
 }
Esempio n. 2
0
  private static Date convertDateTime(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 = getDateTimeFormat().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.getDateTimeFormat().toPattern())));
      }
    } else {
      throw new MolgenisValidationException(
          new ConstraintViolation(
              format(
                  "Attribute [%s] value is of type [%s] instead of [%s] or [%s]",
                  attr.getName(),
                  value.getClass().getSimpleName(),
                  String.class.getSimpleName(),
                  Date.class.getSimpleName())));
    }
    return dateValue;
  }