0

Error:

Unable to parse date from line java.text.ParseException: Unparseable date: "2018-07-26 12.38.35.520"

Code:

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
    return formatter.parse(stringDate);

Tried with and without Local too.

Nick
  • 4,820
  • 18
  • 31
  • 47
Geeta B
  • 3
  • 3
  • 4
    Your input date string has the time components separated by dots, not colons. So maybe try using the mask `yyyy-MM-dd HH.mm.ss.SSS`. – Tim Biegeleisen Jul 27 '18 at 06:24
  • Such a small (BIG?) mistake I made to use ":" instead of ".". – Geeta B Jul 27 '18 at 06:24
  • Tim Beigeleisen, dead eye ,Thanks a lot!! – Geeta B Jul 27 '18 at 06:25
  • `2018-07-26 12.38.35.520` --> `2018-07-26 12:38:35.520` – R. García Jul 27 '18 at 06:25
  • 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. Jul 27 '18 at 08:52
  • Just as a detail, the corresponding exception message from *java.time* will mention the parse position where parsing failed, for example “index 13”, which will help identifying that the mismatch between dot and colon is the problem. – Ole V.V. Jul 28 '18 at 05:52

2 Answers2

1

Your Pattern is not matching your String try the following:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss.SSS", Locale.US);
return formatter.parse(stringDate);
0

input Date is not in correct format. Change it to 2018-07-26 12:38:35.520

Yash
  • 108
  • 1
  • 1
  • 7