-4

I'm trying to parse a date with the format "" to Date.

DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = sdf.parse(dateStr);
String = sdf.format(date);

An example of the dateStr is "2020-04-14 16:34:40.0117372".

I get an error when trying to parse the string, but I don't know why.

The error I'm getting is the following:

java.text.ParseException: Unparseable date: "2020-04-14 16:34:40.0117372"

Why can't I parse this date? How can I do it?

Jens
  • 67,715
  • 15
  • 98
  • 113
Victor
  • 253
  • 2
  • 3
  • 12
  • 3
    The date string contains '-' where the format contains '/'. Simply use the pattern from the title `"yyyy-MM-dd HH:mm:ss"` – Joop Eggen Aug 30 '22 at 11:38
  • 3
    Stop using the outdated classes `java.util.Date` and `SimpleDateFormat`. Switch to modern `java.time` API – Jens Aug 30 '22 at 11:40
  • 1
    Does this answer your question? [String-Date conversion with nanoseconds](https://stackoverflow.com/questions/12000673/string-date-conversion-with-nanoseconds) – Mario Santini Aug 30 '22 at 11:40
  • 1
    You wrote the correct pattern to parse in our question title. Why not actually use that pattern to parse the String to a date? – OH GOD SPIDERS Aug 30 '22 at 11:40
  • 1
    @OHGODSPIDERS Well, almost. It will fail on the milliseconds, I guess – Michael Aug 30 '22 at 11:41
  • @user16320675 Well, it will silently throw them away, but that's still a failure. It's just not an exception. – Michael Aug 30 '22 at 11:45
  • Yes there are 7 fractional seconds (so no microseconds) Not sure about the old time format taking 0117372 as µs and thus with a factor 10 corrupting the seconds. Check that or indeed better use the thorough new java time classes. Read the javadoc – Joop Eggen Aug 30 '22 at 11:45
  • @user16320675 You're assuming the 'user' knows what they are doing when they just asked a question on SO, which seems like borderline madness. Perhaps scoring an internet point against Michael here is not worth it? The remark 'hey, do know that you have millis in your input and this pattern chucks em away' is a useful contribution. – rzwitserloot Aug 30 '22 at 12:18
  • 1
    I came here to comment that 117k milliseconds are actually 117 seconds, which will affect the resulting time, but I see that is already been discussed – Calabacin Aug 30 '22 at 12:26
  • I too strongly recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Also there is *no way* that `SimpleDateFormat` could parse the 7 decimals on the seocnds correctly. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 30 '22 at 17:31
  • Does this in any way answer your question? [java.text.ParseException: Unparseable date: "20:01:00.000Z"](https://stackoverflow.com/questions/56302402/java-text-parseexception-unparseable-date-200100-000z) – Ole V.V. Aug 30 '22 at 17:33

1 Answers1

3

You are using "dd/MM/yyyy" for date format, but you should be using "yyyy-MM-dd" (inverse order, and dashes instead of slashes)

Also I suggest you use modern java.time packages and do something like this:

String str = "2020-04-14 16:34:40.0117372";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSS");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);

Edit: Having 7 digits for milliseconds is correct, first digits for milliseconds and the rest for nanoseconds. strange. Usually you want 3 digits because 1000 milliseconds is a second. You likely have nanoseconds, which should be dealt with by this method.

Calabacin
  • 725
  • 2
  • 8
  • 19
  • 3
    As @Michael said in the comments on the question itself, the input contains milliseconds, but the pattern just chucks em out unbidden. If that's what you want, great. If not, simple matter of adding `.SSSS` to the end of that pattern if memory serves. – rzwitserloot Aug 30 '22 at 12:20
  • Oh I missed that. I have added the milliseconds to the pattern. Thank you @rzwitserloot – Calabacin Aug 30 '22 at 12:22
  • 1
    Good answer (upvoted for the use of java.time). A decimal number can have any number of digits after the decimal point. There is nothing special about `40.0117372`. I have seen times with exactly 7 decimals on the seconds a number of times before. – Ole V.V. Aug 30 '22 at 17:42
  • Apparently the first digits are milliseconds and if you have more they are nanoseconds. In any case having 7 decimal digits is correct. – Calabacin Sep 12 '22 at 16:55