0

I understand that it sounds weird but I have datettime 2018-04-04 12:59:575Z. Let's assume it is real, not a mistake and I can't find any standard for parsing this. Is there a way to parse it in Java? What 3 numbers 575 at the end could mean?

edit: There is strong doubt, that it is correct date time in my samples. I'm going to report a bug to creator. Thanks everyone for good advices.

  • 1
    milliseconds come after seconds. this looks like someone misused a formatter. – Milo Bem Nov 14 '18 at 15:57
  • 1
    Unless you have a huge number of examples confirming the guess that `575` stand for milliseconds, it can be anything. `yyyy-MM-dd HH:mm:SS5z` with literal `5` would also produce similar output. – yegodm Nov 14 '18 at 17:07

4 Answers4

3

It probably means there's a bug in wherever this string came from. I would investigate there if I had access to that code or report a bug to its owner.

There's no point parsing buggy data and guessing what the numbers mean. Bad data is worse than no data.

Milo Bem
  • 1,033
  • 8
  • 20
  • nice idea, but at that time I don't have access to their code –  Nov 15 '18 at 08:51
  • Report a bug then if you can. There's no point parsing buggy data and guessing what the numbers mean. Bad data is worse than no data. – Milo Bem Nov 15 '18 at 14:33
2

What 3 numbers 575 at the end could mean?

My guesses include:

  • It’s 12:59:57.5 (the .5 signifying half a second; assuming that the decimal point has been left out from the format).
  • 575 are millisecond of the second, and seconds have been forgot. So it’s 12:59:ss.575 where we don’t know what ss should have been.
  • It’s 59,575 minutes past 12 o’clock (the same as 12:59:34.5). In defense of this option, ISO 8601 does allow decimals on the minutes, but then the decimal “point” should be either a comma or a period, not a colon.

I can't find any standard for parsing this.

I am pretty convinced that there isn’t any.

Is there a way to parse it in Java?

No, sorry, not as long as we don’t know what the string means.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

There could be two possible options for 59 at the end. It could be minutes or seconds. I think, that minutes is more likely, because seconds could be not valuable. 575 is definitely millisecond.

DateTimeFormatter dfMinutes = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:SSS");
DateTimeFormatter dfSeconds = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:ss:SSS");

P.S. It could be yyyy-dd-MM instead of yyyy-MM-dd. But as I can see, we're in same locale.

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
  • 1
    It'd probably be better to recommend using [`DateTimeFormatter`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/DateTimeFormatter.html) from the newer `java.time` API. – Slaw Nov 14 '18 at 16:28
  • 1
    It's not so much the old date-time API doesn't work, it's that the new date-time API is better in pretty much every way. Since OP gives no indication of already using the old API, it seems more appropriate to direct him towards the more modern, better implemented API. – Slaw Nov 14 '18 at 16:45
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Nov 14 '18 at 17:54
  • 1
    For starters, count the questions on Stack Overflow about unexpected results from `SimpleDateFormat`. Other relevant Stack Overflow questions: [Why is the Java date API (java.util.Date, .Calendar) such a mess?](https://stackoverflow.com/questions/1571265/why-is-the-java-date-api-java-util-date-calendar-such-a-mess) and [What's wrong with Java Date & Time API?](https://stackoverflow.com/questions/1969442/whats-wrong-with-java-date-time-api) – Ole V.V. Nov 14 '18 at 18:12
0

You can use joda time api to parse the input String like below:-

String strDate="2018-04-04 12:59:575Z";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:SSSZ");
DateTime dt = formatter.parseDateTime(strDate);
System.out.println(dt); //2018-04-04T08:59:00.575-04:00

575 in your input string is milliseconds.

But you need to find out whats the point of precision till milliseconds if you are not including seconds.

Amit Bhati
  • 5,569
  • 1
  • 24
  • 45