user1679671
user1679671

Reputation:

How to parse datetime like this

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.

Upvotes: 0

Views: 191

Answers (4)

Milo Bem
Milo Bem

Reputation: 1033

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.

Upvotes: 3

Anonymous
Anonymous

Reputation: 86272

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.

Upvotes: 2

Oleg Cherednik
Oleg Cherednik

Reputation: 18245

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.

Upvotes: 0

Amit Bhati
Amit Bhati

Reputation: 5649

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.

Upvotes: 0

Related Questions