Amila Iddamalgoda
Amila Iddamalgoda

Reputation: 4286

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"

Upvotes: 6

Views: 33380

Answers (3)

Levite
Levite

Reputation: 17617

In order to not just get a vaild timestamp format, but also have zero offset from UTC use

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

Otherwise / Pitfalls

When not explicitly specifying the timestamp as UTC, zero offset from local time might be assumed. Also while following might be a valid pattern, it is somewhat misleading.

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

As described by Meno, this treats the zero time zone offset as a literal (therefore ignoring it).

Also considers pumping up Meno Hochschild's answer for explaining this in better detail, since I am not allowed to delete mine for now (accepted answer).

Upvotes: -3

William Price
William Price

Reputation: 4102

From the v1.6 API documentation:

'Z' outputs offset without a colon, 'ZZ' outputs the offset with a colon, 'ZZZ' or more outputs the zone id.

When you specify Z (without single quotes) in your pattern, the value in your timestamp must be in the format +HHMM or -HHMM as a numeric offset from UTC. The literal character Z is not valid input for the specified format.

Examples:

  • 2014-09-23T23:03:11+0000
  • 2014-09-23T23:03:11-0500
  • 2014-09-23T23:03:11+0430

As Levit mentioned in the other answer, if the goal is to accept a literal 'Z' in the input timestamp without treating it as a time zone (bad idea) then the Z character can be quoted using single quotes in the pattern (...'Z'). That is similar to what was done for the literal 'T' that separates the date components from the time components. Treating Z in the input as a literal is not recommended because it has meaning and, if provided, the time zone is an important component of the timestamp.

Upvotes: 7

Meno Hochschild
Meno Hochschild

Reputation: 44061

I am very sceptical about treating Z just as literal. The char Z has a meaning, namely zero offset. The documentation of Joda-Time version 1.6 says about this code:

String timestamp = "2014-09-23T23:03:11Z";
DateTime dt = 
  ISODateTimeFormat.dateTimeNoMillis().parseDateTime(timestamp).withZone(DateTimeZone.UTC);
System.out.println(dt); // 2014-09-23T23:03:11.000Z

Returns a formatter that combines a full date and time without millis, separated by a 'T' (yyyy-MM-dd'T'HH:mm:ssZZ). The time zone offset is 'Z' for zero, and of the form '±HH:mm' for non-zero.

Now let's view at following four alternatives in detail (explicitly tested with version 1.6.2):

String timestamp = "2014-09-23T23:03:11Z";
DateTimeZone utc = DateTimeZone.UTC;

DateTime dt1 = ISODateTimeFormat.dateTimeNoMillis().parseDateTime(timestamp).withZone(utc);
System.out.println(dt1); // 2014-09-23T23:03:11.000Z (OK)

DateTime dt2 = new DateTime(timestamp, utc);
System.out.println(dt2); // 2014-09-23T23:03:11.000Z (OK)

DateTime dt3 =
  DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").parseDateTime(timestamp).withZone(utc);
System.out.println(dt3); //2014-09-23T21:03:11.000Z (WRONG!!!)

DateTime dt4 =
  DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZZ").parseDateTime(timestamp).withZone(utc);
// exception: Invalid format: "2014-09-23T23:03:11Z" is malformed at "Z"

Conclusion: The other answers given so far treating Z as literal are wrong because the input is treated in local timezone, not with offset UTC+00:00. Use either the constructor or the specific class IsoDateTimeFormat (I would prefer latter one for clarity).

About the exception: This is a bug solved with version 2.0, see release-notes. You should better update your library version.

Allow 'Z' and 'ZZ' in format patterns to parse 'Z' as '+00:00' [2827359]

Upvotes: 10

Related Questions