Amila Iddamalgoda Amila Iddamalgoda - 6 months ago 46
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.

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

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

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


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

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).