1

I'm trying to parse an String into a java.util.Date.

Currently, I'm using SimpleDateFormat, with the "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" format String, and it works pretty well most of the time; for example, those work okay:

  • "2022-03-16T12:09:56.267Z"
  • "2022-03-16T12:11:55.017+03:00"

The problem lies with perfectly valid ISO strings that happen to use less than three digits for the miliseconds:

  • "2022-03-16T09:18:31.9Z"

It throws this exception: java.text.ParseException: Unparseable date: "2022-03-16T09:18:31.9Z".

Is there a way to handle those? Please, do keep in mind that I need to return a java.util.Date, but using SimpleDateFormat is optional.

I'm using Java 8.

Haroldo_OK
  • 6,612
  • 3
  • 43
  • 80
  • Please describe "the problem lies [...]". What problem? What do you see when you try to parse that String with the format you have above? It parses fine for me: https://ideone.com/Iam9Pt – Savior Mar 16 '22 at 13:15
  • Added the exception to the description. – Haroldo_OK Mar 16 '22 at 13:19
  • Please post a [MCVE]. I can't repro that. – Savior Mar 16 '22 at 13:20
  • 1
    `new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").parse("2022-03-16T09:18:31.9Z")` works for me using Java 17; it does **not** work with Java 8 – user16320675 Mar 16 '22 at 13:21
  • @user16320675 From `new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").parse("2022-03-16T09:18:31.9Z").getTime()` I expect 1647422311900 but get 1647422311009 (using Java 11). – Ole V.V. Mar 16 '22 at 13:26
  • 1
    `SimpleDateFormat` (when before Java 8 it was reasonable to use it) never supported one or two digits of fraction of second, only three decimals. Related: [SimpleDateFormat showing incorrect milliseconds with "S" format, but not with "SSS"](https://stackoverflow.com/questions/67535630/simpledateformat-showing-incorrect-milliseconds-with-s-format-but-not-with-s). – Ole V.V. Mar 16 '22 at 13:28
  • 2
    note: "works for me" on last comment should only be interpreted as "does not throw an Exception" - I have not checked the result nor do I *recommend* the use of `SimpleDateFormat` - it was just a complement to first [comment](https://stackoverflow.com/questions/71497315/simpledateformat-iso-having-problems-parsing-the-milliseconds-if-they-have-less#comment126370213_71497315) on this question (with link to IDEone) – user16320675 Mar 16 '22 at 13:29

1 Answers1

3

Here is one way.

  • Note the Z stands for Zulu.
  • And also remember that Date does not store any time zone information.
  • If necessary, you can modify the ZonedDateTime instance before converting to Date.
Instant d = Instant.parse("2022-03-16T09:18:31.9Z");
Date date = Date.from(d);
System.out.println(d);
System.out.println(date);

prints

2022-03-16T09:18:31.900Z
Wed Mar 16 05:18:31 EDT 2022

I would recommend that you try to convert to using the classes in the java.time package as they are quite superior to Date.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
WJS
  • 36,363
  • 4
  • 24
  • 39
  • 1
    If the requirement to return an old-fashioned `Date` is indispensable, then this is the good way. Thanks. Only I’d parse directly into an `Instant` and save the first conversion. Use `Instant.parse("2022-03-16T09:18:31.9Z")`. – Ole V.V. Mar 16 '22 at 13:51