12

In Java 8 time / JSR 310 / threeten.org backport ...

Q: How do I parse ISO-8601 timestamps with either a 'T' or a space ' ' between the date portion and the time portion?

ISO-8601 format for timestamps specifies the literal 'T' to separate the date specification from the time specification:

2015-05-12T15:42:00.123

Many applications generate timestamps in this format with a space ' ' char instead of 'T'. References indicate that ISO-8601 allows this by mutual agreement.

2015-05-12 15:42:00.123

Because I am ingesting data from multiple sources I want to allow either the 'T' or the space ' '.

I observe that the pattern string allows one to specify optional components, but I do not see any way to specify a "choice" in a pattern string ...

Q: Is there any way to make a "choose-exactly-one-of-the-following" in a JSR 310 pattern string?

I was able to get this to work by constructing a DateTimeFormatter with two optional patterns:

DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
    .appendOptional(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
    .optionalStart().appendPattern("yyyy-MM-dd HH:mm:ss.SSS").optionalEnd()
    .toFormatter();

Seeing optionalStart() and optionalEnd() led me to:

DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
    .appendPattern("[yyyy-MM-dd HH:mm:ss.SSS][yyyy-MM-dd'T'HH:mm:ss.SSS]")
    .toFormatter();

However, I am not confident that this is the correct way to deal with this situation ...

Q: What is the best-practice solution to handle multiple DateTime patterns?

Thanks in advance.

4
  • 1
    Please try to condense your three questions into one, for clarity's sake. Other than that, a very good question.
    – anon
    May 13, 2015 at 0:16
  • 4
    Just to note that java.time has no specific way to handle two options during parsing, such as " " or "T". This would be a reasonable enhancement to the API. The two solutions above will both successfully parse an empty string or a string with two date-times, so you need to check for those. May 13, 2015 at 6:32
  • > will both successfully parse an empty string or a string with two date-times I was concerned about parsing 2 dates ... did not realize that an empty string would also pass. I will write some more tests. Thanks. May 13, 2015 at 20:48
  • I found this SO most helpful: stackoverflow.com/a/26132715/144599
    – davidemm
    Feb 23, 2021 at 22:22

1 Answer 1

8

Cleanse Your Input Data

My own practice:

When accepting text from outside my own code, I never trust such external inputs. Heck, I don’t even trust my own internal inputs. I always put such data through a cleaner. As part of that cleaning I replace any SPACE character in any expected ISO 8601 string with a T.

String datetimeInput = input.replace( " ", "T" ) ;

I also make a habit of calling the Google Guava library to trim whitespace from the input string. See this Answer for details.

And check for NULL, non-printing, and other inappropriate characters. Those non-printables make for nasty bugs.

Only after that do I call the date-time libraries ( Joda-Time or java.time ).

Trap Parsing Exception

Also, you should use a try-catch to trap for the parsing exception to more gracefully handle when the date-time input string fails to meet your expectations.

1
  • > I replace any SPACE character in any expected ISO 8601 string with a T This is good advice and seems to be the cleanest way to handle this problem. May 13, 2015 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.