意外行为 DateFormat

huangapple go评论115阅读模式
英文:

Unexpected behavior DateFormat

问题

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:
意外行为 DateFormat

英文:

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:
意外行为 DateFormat

答案1

得分: 3

SimpleDateFormat不是线程安全的。因此在几乎所有情况下,将其用作静态常量并不是一个好主意。最好在每次需要时创建一个新实例。只需参考这里

英文:

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

huangapple
  • 本文由 发表于 2020年8月24日 22:01:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63562604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定