Amila Iddamalgoda Amila Iddamalgoda - 16 days ago 7
Java Question

Validating Timestamp format yyyy-MM-dd'T'HH:mm:ssZ in java?

I'm trying to do a timestamp validation using joda time-1.6.2. Please point my error and help me out.
Code

String timestamp = "2014-09-23T23:03:11Z";
String datePattern = "yyyy-MM-dd'T'HH:mm:ssZ";

try {
DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(datePattern);
dateFormatter.parseDateTime(timestamp);

} catch (Exception e) {
LOG.info("Timestamp is invalid format" + e);
}


Exception

INFO: Timestamp is invalid formatjava.lang.IllegalArgumentException: Invalid format: "2014-09-23T23:03:11Z" is malformed at "Z"

Answer
String timestamp = "2014-09-23T23:03:11Z";
DateTime dt = new DateTime(timestamp, DateTimeZone.UTC);

Original Answer

String timestamp = "2014-09-23T23:03:11Z";
String datePattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";

Note the single quotes on Z which are necessary if your timestamp string ends with a Z (othewise you can leave them out).