6

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"
Amila Iddamalgoda
  • 4,166
  • 11
  • 46
  • 85

3 Answers3

10

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]

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • +1 for calling out that treating 'Z' as a literal is bad because it does not interpret it as a time zone that way. I assumed the reader would recognize that but have updated my answer. – William Price Sep 24 '14 at 17:02
  • +1 Thx for pointing this out, updated my answer as well! Yours seems to be the most complete answer/explanation. – Levite Sep 25 '14 at 07:30
  • @Meno Hochschild just question, is it a malformed format? 2015-11-17T00:00:00+0000Z , is possible to have +0000 and Z at same time? – Zilev av Mar 07 '16 at 23:38
  • 1
    @Zilevav That is double information (redundant) and would require two pattern symbols for parsing (must be different - something like "zXXX" in `SimpleDateFormat`, "X" is not supported by Joda-Time). Not a good idea. Worse, what will you do if the offset in input is ambivalent (for example "...+01:00Z")? `SimpleDateFormat` just uses the first offset "+01" and ignores the second (Z = "00:00"), even in strict mode, tested by me. – Meno Hochschild Mar 08 '16 at 05:58
7

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.

Community
  • 1
  • 1
William Price
  • 4,033
  • 1
  • 35
  • 54
  • +1 This also provides a good explanation/description regarding the meaning of the `Z` character! – Levite Sep 25 '14 at 09:12
-3

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

Levite
  • 17,263
  • 8
  • 50
  • 50