I'm receiving really strange date format from server. It looks 2017-03-07T15:08:01.513544Z , and strange part is six last characters before "Z", because I've seen only three symbols of milliseconds in the most responses from servers. I'm trying to parse it with SimpleDateFormat
with the mask yyyy-MM-dd'T'HH:mm:ss.SSS'Z', and it works on all android versions except api 16 (4.1). In developer.android.com I saw example with the mask like .SSSXXX, but as result I have error, because system doesn't know symbol 'X'. So, have you ever faced with similar date format?

- 796
- 5
- 20

- 1,697
- 2
- 19
- 31
-
Could you [edit] your question? You can improve formatting and adjust the question to [mcve] and [ask] – xenteros May 17 '17 at 11:03
-
this type of date format will be in Calendar like Outlook invitation – Mohammad nabil May 17 '17 at 11:12
-
"yyyyMMdd'T'HHmm'00'" I have used like this – Mohammad nabil May 17 '17 at 11:13
-
Mostly correct, @tommus. Java 8 has nanosecond precision, so not a microsecond will be lost. – Ole V.V. May 17 '17 at 12:53
-
The IBM mainframe I was working with, used to give us time stamps with 6 decimals on the seconds like yours. The format you mention conforms with ISO 8601, so there should be plenty of options for parsing it. – Ole V.V. May 17 '17 at 12:55
-
See also [Java date parsing with microsecond or nanosecond accuracy](http://stackoverflow.com/questions/30135025/java-date-parsing-with-microsecond-or-nanosecond-accuracy) and [java.util.Date format SSSSSS: if not microseconds what are the last 3 digits?](http://stackoverflow.com/questions/19223171/java-util-date-format-ssssss-if-not-microseconds-what-are-the-last-3-digits) – Ole V.V. May 17 '17 at 12:57
1 Answers
The answer is straightforward but entails a little more than just the answer.
Instant instantFromServer = Instant.parse("2017-03-07T15:08:01.513544Z");
To use this on Android you will need to get the ThreeTenABP library. It contains the date and time classes described in JSR-310 of which Instant
is one. See the links below.
So, have you ever faced with similar date format?
01.513544
means 1.513544 seconds, or 1 second 513 milliseconds 544 microseconds, or 1 second 513544000 nanoseconds. There are many ways to put it. I have worked with IBM mainframes that routinely gave us timestamps with microsecond precision, that is, 6 decimals on the seconds. I suppose as computers are getting faster, accuracy requirements are getting stricter, so we may see more of these in the future. While I believe that SimpleDateFormat
cannot handle these, the classes in JSR-310 generally have nanosecond precision and parse strings with variable number of decimals out of the box.