Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
like image 822
Amila Iddamalgoda Avatar asked Sep 24 '14 06:09

Amila Iddamalgoda


People also ask

What date is yyyy mm DDThh mm SSZ?

"YYYY-MM-DDThh:mm:ss-hh:mm" is ISO 8601 format.

What is Z in timestamp Java?

The Z stands for the Zero timezone, as it is offset by 0 from the Coordinated Universal Time (UTC).

What is TZ format?

The tz database is published as a set of text files which list the rules and zone transitions in a human-readable format. For use, these text files are compiled into a set of platform-independent binary files—one per time zone.


2 Answers

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]

like image 166
Meno Hochschild Avatar answered Sep 19 '22 23:09

Meno Hochschild


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.

like image 20
William Price Avatar answered Sep 17 '22 23:09

William Price