1

I have to parse a date time String being sent over json - using Java 7, with the date time being in this format :

2016-04-26T11:35:30.0543787Z

The problem I'm having is with the extra decimals in the seconds - I can parse it if it is only to 3 decimal places e.g. milliseconds.

I can parse the value using SimpleDateFormat and the format string
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" but this gives an incorrect value.

Any help would be appreciated!

Tejal
  • 764
  • 1
  • 10
  • 39
flinthart
  • 534
  • 1
  • 4
  • 14
  • Use `SSSSSSS`...? – Jai Jul 24 '18 at 06:33
  • 1
    Jai - I tried that - it returns an incorrect date – flinthart Jul 24 '18 at 06:40
  • You can always do pre-processing using regex/string methods. – Jai Jul 24 '18 at 06:41
  • It can't be done with SimpleDateFormat while keeping all of the precision. `SimpleDateFormat::parse` returns a Date, which only has millisecond precision. You need microsecond precision. You'll have to either find another parsing library, or else parse the microseconds separately (using regex or such to extract them) and combine them with the rest of the value. – yshavit Jul 24 '18 at 06:41
  • yshavit - Thanks for that - yeah I figured as much - I'm happy to use another library but I haven't been able to find anything so far... I guess I'll just pre-process the String before parsing and remove the microseconds - I don't really care about keeping the time to that precision – flinthart Jul 24 '18 at 06:47
  • @flinthart I found another question on SO that seems to answer this. If it's not a duplicate, please let me know (tag me with `@yshavit` so I get a notification) and I can re-open this. – yshavit Jul 24 '18 at 06:59
  • @yshavit It's not a duplicate... The question says nothing about having to be Java 7, and the answer is all about Java 8. – kumesana Jul 24 '18 at 07:59
  • 1
    Never put 'Z' in your date format patterns. Z is not a separator marker letter like T is. Z indicates that the timestamp is expressed Zulu time, that is to say as GMT. If you put 'Z' in your date format pattern, the parser will ignore this information that the timestamp is in GMT, and therefore it will select whatever default time zone it will see fit, typically the current computer's system time zone. Which is incorrect and produces incorrect results. – kumesana Jul 24 '18 at 08:04
  • @flinthart I agree that your question is not a strict duplicate of the linked one, but that one does seem to me to answer your question too, doesn’t it? Could you specify which information you are still missing? – Ole V.V. Jul 24 '18 at 08:10
  • 1
    [java.time, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) has been backported to Java 7 (and even Java 6). `Instant.parse("2016-04-26T11:35:30.0543787Z")` parses your string just like that with all decimals coming through and even without any explicit formatter. I certainly recommend taking a look at [the ThreeTen Backport project](http://www.threeten.org/threetenbp/), the backport of `java.time` to Java 6 and 7 (ThreeTen for JSR-310, where java.time was first described). – Ole V.V. Jul 24 '18 at 08:17
  • @kumesana Good catch about the Java 7/8, but the _question_ that I've linked as a dupe doesn't talk about Java 8 specifically. The (sole, accepted) answer says that it's not possible with Java 7 JDK classes, and is possible with 8. If that answer is inaccurate, that doesn't mean that question isn't a dupe -- it means we should post another answer or suggest edits to the accepted one. – yshavit Jul 24 '18 at 13:56
  • 2
    @yshavit [The answer to the linked question](https://stackoverflow.com/a/30135772/5772882) has now been edited, information for Java 7 (and 6) has been added. – Ole V.V. Jul 24 '18 at 19:28

1 Answers1

1

Since you are not bother about the precision of the milliseconds, we can remove the decimals from the seconds.

**NB:**And the output would depend on the current timezone of your server/machine. That's why the you felt like the output is different from what you expected.

        String time = "2016-04-26T11:35:30.0543787Z";
        int l = time.lastIndexOf('.');
        time = time.substring(0, l);
        time = time+"Z";
        System.out.println(time);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
         try {

             Date date = dateFormat.parse(time.replaceAll("Z$", "+0000"));
             System.out.println(date);

             System.out.println("time zone : " + TimeZone.getDefault().getID());
             System.out.println(dateFormat.format(date));

         } catch (ParseException e) {
             e.printStackTrace();
         }
RKA
  • 308
  • 2
  • 10
  • 1
    FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. Much of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Jul 24 '18 at 19:14