// parses a datetime string that adhears to the given pattern and returns a java.util.Date object public static java.util.Calendar parse(String input, String pattern) throws IllegalArgumentException { if (pattern == null) pattern = DEFAULT_DATETIME_PATTERN; java.util.Calendar output = null; if (input != null) { if (pattern.equals("datetime")) { output = javax.xml.bind.DatatypeConverter.parseDateTime(input); } else if (pattern.equals("date")) { output = javax.xml.bind.DatatypeConverter.parseDate(input); } else if (pattern.equals("time")) { output = javax.xml.bind.DatatypeConverter.parseTime(input); } else if (pattern.equals("milliseconds")) { output = java.util.Calendar.getInstance(); output.setTimeInMillis(Long.parseLong(input)); } else { java.text.DateFormat formatter = new java.text.SimpleDateFormat(pattern); formatter.setLenient(false); output = java.util.Calendar.getInstance(); try { output.setTime(formatter.parse(input)); } catch (java.text.ParseException ex) { throw new IllegalArgumentException(tundra.exception.message(ex)); } } } return output; }
@Override public Object convert(Object value) { try { DatatypeConverter.parseDate((String) value); } catch (Exception ex) { LOG.error(ex.getMessage(), ex); LOG.error("Failed to parse date", ex); throw new EntityValidationException( ex, "irrelevant", "doesn't matter", new ArrayList<ValidationError>()); } return value; }
/** * Validates the given entity Returns true if the validation was successful or a * ValidationException if the validation was unsuccessful. * * @param fieldName name of entity field being validated * @param entity being validated using this SLI Schema * @param errors list of current errors * @param repo reference to the entity repository git status * @return true if valid */ @Override protected boolean validate( String fieldName, Object entity, List<ValidationError> errors, Repository<Entity> repo) { boolean isValid; try { javax.xml.bind.DatatypeConverter.parseDate((String) entity); isValid = true; } catch (IllegalArgumentException e2) { isValid = false; } return addError( isValid, fieldName, entity, "RFC 3339 Date", ErrorType.INVALID_DATE_FORMAT, errors); }
/** * @param s * @return */ public static Date parseDate(String s) { if (s != null && !s.trim().isEmpty()) { return DatatypeConverter.parseDate(s).getTime(); } return null; }