Checking the timestamp format of yyyy-MM-dd'T'HH: mm: ssZ in java?

I am trying to validate timestamp using joda time-1.6.2 . Please indicate my mistake and help me. the 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" 
+6
source share
3 answers
 String timestamp = "2014-09-23T23:03:11Z"; DateTime dt = new DateTime(timestamp, DateTimeZone.UTC); 

Original (incorrect) answer

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

This, however, did treat the β€œtime zone zero offset” as a literal and therefore ignored it, a newer answer now works for all cases (see Meno Hochschild, answer for more details).

-2
source

I am very skeptical about considering Z as literally. char Z matters, namely zero offset. The Joda-Time documentation version 1.6 talks 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 the full date and time without milliseconds separated by "T" (yyyy-MM-dd'T'HH: mm: ssZZ). The time zone offset is β€œZ” for zero and the shape is β€œΒ± HH: mm” for non-zero.

Now let's look at the following four alternatives (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 that have so far treated Z as a literal are incorrect because the input is processed in the local time zone, and not with an offset of UTC + 00: 00. Use the constructor or the concrete class IsoDateTimeFormat (I would prefer the latter for clarity).

About the exception: This is a bug fixed with version 2.0, see the release notes . You better upgrade your library version.

Allow 'Z' and 'ZZ' in template formats to parse 'Z' as '+00: 00' [2827359]

+8
source

From v1.6 API documentation :

'Z' gives an offset without a colon, 'ZZ' displays an offset with a colon, 'ZZZ or more displays a zone identifier.

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

Examples:

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

Like Levit mentioned in another answer, if the goal is to accept the literal β€œZ” in the timestamp of the input without treating it as a time zone (bad idea), then the Z character can be specified using single quotes in the pattern ( ...'Z' ) This is similar to what was done for the literal β€œT”, which separates the date components from the time components. Processing Z at the input as a literal is not recommended because it matters and, if provided, the time zone is an important component of the timestamp.

+6
source

Source: https://habr.com/ru/post/1203279/


All Articles