I'm a bit baffled what format these timestamps are in. I was told the format to use is yyyy-MM-dd.HH:mm:ss
but all of the timestamps appear like this 2017-01-01 00:08:57.231
, 2017-01-01 07:43:36.348
, or 2017-01-01 13:25:55.683
. I'm not understanding why there are four sections to the time ?:Hour:Minute:Second
in the actual data I have when the format I'm supposed to be using only has three time sections. Are these datetime timestamps not actually in the format of yyyy-MM-dd.HH:mm:ss
?

- 6,055
- 11
- 62
- 100
-
I'm not sure I understand what is your question... – Liora Haydont Apr 05 '18 at 19:11
-
Well, there is no dot between the date and the time, and there are milliseconds after the seconds. So how coult it match the format `yyyy-MM-dd.HH:mm:ss`, which *has* a dot between date and time, and which *doesn't* have milliseconds after the seconds? – JB Nizet Apr 05 '18 at 19:11
-
2`2017-01-01 13:25:55.683` and `2017-01-01 00:08:57.231` are in the format `yyyy-MM-dd.HH:mm:ss.SSS` the trailing 3 `S`s are fractional seconds – xtratic Apr 05 '18 at 19:11
-
@xtratic I still can't seem to parse the strings 2017-01-01 13:25:55.683 to a date using the format yyyy-MM-dd.HH:mm:ss.SSS. – Kyle Bridenstine Apr 05 '18 at 19:17
-
@xtratic SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd.HH:mm:ss:SSS"); formatter.parse("2017-01-01 13:25:55.683"); throws Unparseable date: "2017-01-01 13:25:55.683" – Kyle Bridenstine Apr 05 '18 at 19:17
-
@Mr.Tea sorry, my format was botched in the first comment, since I just copied and used yours as a template. The right format is `yyyy-MM-dd HH:mm:ss.SSS` . But please learn to read the Javadoc as @JB Nizet says, all this information is right there. – xtratic Apr 05 '18 at 19:20
-
@Mr.Tea pay attention. Again, you have a dot in your pattern between the date and the time, but there is no dot in the input. And the milliseconds are separated by a dot in the input, not by a colon as in your pattern. You seem to be trying random things instead of reading the comments you got and the javadoc and applying common sense. – JB Nizet Apr 05 '18 at 19:21
-
@JB Nizet please comment above this one. – Kyle Bridenstine Apr 05 '18 at 19:23
-
@Mr.Tea The issue was that you originally posted `yyyy-MM-dd.HH:mm:ss` we didn't notice the `.` rather than ` ` (space) in the middle and just added the `.SSS` part. – xtratic Apr 05 '18 at 19:25
-
@xtratic I see that I'm just saying the people I got this data from explicitly said it was in the format yyyy-MM-dd.HH:mm:ss and it's not. – Kyle Bridenstine Apr 05 '18 at 19:26
-
Ah, gotcha. Yep, that happens quite a bit actually. Lies about formats and broken standards all over. – xtratic Apr 05 '18 at 19:28
-
@Mr.Tea I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 05 '18 at 20:06
2 Answers
No, your suspicion is correct, your example date-time strings are not in the format yyyy-MM-dd.HH:mm:ss
. The dot between dd and HH must be a simple mistake, it should be a space since there is a space between date and time in the timestamp strings. Furthermore all of your example strings include milliseconds: in 00:08:57.231 you’ve got 57 seconds and 231 milliseconds, or if you like, 57.231 seconds, so the last section may also be referred to as fraction of second.
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
String timestampString = "2017-01-01 00:08:57.231";
LocalDateTime dateTime = LocalDateTime.parse(timestampString, formatter);
System.out.println(dateTime);
Output:
2017-01-01T00:08:57.231
For the nerdily curious: It is possible to parse the string, or more precisely most of it, with the format you had been given, only correcting the dot into a space:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String timestampString = "2017-01-01 00:08:57.231";
LocalDateTime dateTime = LocalDateTime.from(
formatter.parse(timestampString, new ParsePosition(0)));
In this case the result comes without the milliseconds:
2017-01-01T00:08:57
I see no reason why you should want this, though.
Avoid SimpleDateFormat
In a comment you gave a snippet that uses the SimpleDateFormat
class. This class is not only long outdated, it is also notoriously troublesome. I see no reason why you should want to use it. Instead I am using java.time
, the modern Java date and time API. In my experience it is so much nicer to work with.
Links
Oracle tutorial: Date Time explaining how to use java.time
. You may especially want to study the section Parsing and Formatting.

- 81,772
- 15
- 137
- 161
These time stamps are in yyyy-MM-dd HH:mm:ss:ms
format, last three digits are milliseconds.

- 81,772
- 15
- 137
- 161

- 89
- 1
- 9
-
Why the downvote? This answer may be short and not very well explained, but it’s at least to the point and perfectly correct. – Ole V.V. Apr 05 '18 at 19:40