Manuel Jordan
Manuel Jordan

Reputation: 16289

String Format works fine through Java's SimpleDateFormat and not with JodaTime's DateTimeFormat

I am working with Hibernate Validator.

I have a java.util.Date object and it is retrieved through reflection how a String due the following:

Object dateDeathObject = BeanUtils.getProperty(value, "dateDeath");
logger.info("dateDeathObject: {}", dateDeathObject);

The output always has the following format:

- dateDeathObject: Mon Sep 01 16:01:42 PET 2014

Using the SimpleDateFormat API

The pattern is EEE MMM dd hh:mm:ss zzz yyyy

Therefore with JDK 8 from the String (that represents the Date.toString() of some Date value) I can get again the original Date representation object to perform later some validation

SimpleDateFormat sdf =  new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy");
Date date = sdf.parse((String) dateDeathObject);
logger.info("date: {}", date);

And works fine

I can see in the output the following:

The problem is with JodaTime, how an alternative to use java.util.Date.

DateTimeFormatter formatter = DateTimeFormat.forPattern("EEE MMM dd hh:mm:ss zzz yyyy");
DateTime dt = formatter.parseDateTime( (String) dateDeathObject );
logger.info("dt: {}", dt);

Observe the same EEE MMM dd hh:mm:ss zzz yyyy pattern, but I get instead the following error:

javax.validation.ValidationException: HV000028: Unexpected exception during isValid call.
...
Caused by: java.lang.IllegalArgumentException: Invalid format: "Mon Sep 01 16:01:42 PET 2014" is malformed at "PET 2014"
    at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:873)
    at com.manuel.jordan.validation.support.DateDeathValidator.isValid(DateDeathValidator.java:67)
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateSingleConstraint(ConstraintTree.java:283)
    ... 38 more

I am confused about is malformed at "PET 2014". What is missing with JodaTime?

Upvotes: 0

Views: 289

Answers (1)

hd1
hd1

Reputation: 34677

PET is not listed in the list of joda-time supported timezones. You might try updating the database. Hope that helps, leave a comment if you're still having trouble.

Upvotes: 1

Related Questions