0

I'm facing a strange behavior of the DateFormat in Java. I declared a DateFormat like this :

    private static final DateFormat     DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH");

But I got an exception on this line

    Date date = DATE_FORMAT.parse(rawDate); rawDate = "2020-08-08 12"
Caused by: java.lang.NumberFormatException: For input string: ".241822E"
    at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054) ~[na:na]
    at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110) ~[na:na]
    at java.base/java.lang.Double.parseDouble(Double.java:549) ~[na:na]
    at java.base/java.text.DigitList.getDouble(DigitList.java:169) ~[na:na]
    at java.base/java.text.DecimalFormat.parse(DecimalFormat.java:2198) ~[na:na]
    at java.base/java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:2241) ~[na:na]
    at java.base/java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1542) ~[na:na]
    at java.base/java.text.DateFormat.parse(DateFormat.java:394) ~[na:na]
    at com.livingobjects.collector.plugin.sfr.archipel.AstelliaDebit4GPlugin.handle(AstelliaDebit4GPlugin.java:109) ~[classes/:na]
    ... 7 common frames omitted

Which makes no sense at all... What's great is that I can try to exec the same line on debugger, and it works: enter image description here

locke14
  • 1,335
  • 3
  • 15
  • 36
Erwan Daniel
  • 1,319
  • 11
  • 26
  • 1
    I agree that makes no sense. Are you absolutely positive `rawDate` contains the value you think it contains? – Federico klez Culloca Aug 24 '20 at 14:03
  • 1
    Are you sure you want an offset / time zone being added automatically without controlling it yourself? If you don't and you want to avoid several other problems, use `java.time`. Hint: the time of day is not the one from `rawData` (12 != 13). – deHaar Aug 24 '20 at 14:10
  • Values in your code here and debugger does not tally. – Pubudu Sitinamaluwa Aug 24 '20 at 14:10
  • 2
    Works fine for me: System.out.println(new SimpleDateFormat("yyyy-MM-dd HH").parse("2020-08-08 12")); (Anyway you should stop using the old classes like SimpleDateFormat and switch to the java-8 date/time classes) – TomStroemer Aug 24 '20 at 14:10
  • 1
    @deHaar correct, the image shown is from another attempt. – Erwan Daniel Aug 24 '20 at 14:16
  • @TomStroemer But those old classes are usefull when the input format is not a common one – Erwan Daniel Aug 24 '20 at 14:20
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. 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 25 '20 at 17:03
  • @ErwanDaniel Beg to differ. The modern `DateTimeFormatter` offers even more possibilities for non-common formats that the old and troublesome `SimpleDateFormat`. Set `DATE_FORMAT` to `DateTimeFormatter.ofPattern("yyyy-MM-dd HH")`, and `LocalDateTime.parse(rawDate, DATE_FORMAT)` will give you a `LocalDateTime` of `2020-08-08T12:00`. – Ole V.V. Aug 25 '20 at 17:05
  • @OleV.V. Well, I missed it. But sure now I use it. My only deception is the miss of a beautiful conversion to the old Date class. The framework I use is using Date. Si I have to convert to Instant: `Date.from(date.toInstant(ZoneId.systemDefault().getRules().getOffset(date)))`. Didn't find a prettier way. – Erwan Daniel Aug 26 '20 at 13:24
  • 1
    I’d use the slightly shorter `Date.from(date.atZone(ZoneId.systemDefault()).toInstant())`. It’s still a bit wordy, but has the advantage of making it explicit that the default time zone is used, a pretty crucial piece of information (assuming `date` is a LocalDateTime`). – Ole V.V. Aug 26 '20 at 15:02

1 Answers1

3

SimpleDateFormat is not thread safe. So using it as a static constant is not a good idea in almost all cases. Better create a new instance every time you need it. Just see here

MaS
  • 393
  • 3
  • 17
  • No way... Good guess ! It's the answer. I putted it in static for performence.. lol – Erwan Daniel Aug 24 '20 at 14:14
  • 1
    I agree about the diagnosis, not about the cure. Instead I strongly recommend using `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 25 '20 at 17:10