Java中的奇怪日期解析错误

时间:2014-12-28 08:00:19

标签: java parsing date datetime simpledateformat

在解析java中的大量日期时,我偶尔会遇到这个奇怪的错误:

java.lang.NumberFormatException: For input string: ".201144E4.201144E4"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1250)
    at java.lang.Double.parseDouble(Double.java:540)
    at java.text.DigitList.getDouble(DigitList.java:168)
    at java.text.DecimalFormat.parse(DecimalFormat.java:1321)
    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1793)
    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1455)
    at java.text.DateFormat.parse(DateFormat.java:355)
    at gameloop.tf2tradebot.user.UserManager.getUser(UserManager.java:102)
    at gameloop.tradebot2.bot.weaponbot1.Weaponbot1.onMessageReceived(Weaponbot1.java:269)
    at gameloop.api.steam.chat.ChatEvent.run(ChatEvent.java:49)
    at java.lang.Thread.run(Thread.java:745)

我是这样的,日期是

2014-12-13 06:56:27

日期格式为

private static final DateFormat STANDARD_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);

我的代码:

Date firstSeenDate = null;
try
{
    firstSeenDate = STANDARD_DATE_FORMAT.parse(firstSeen);
}
catch(Exception pe)
{
    pe.printStackTrace();
    logger.outputError(4001, "Error parsing first seen date. Shutting down...");
    logger.outputError(4001, "First seen date : \'" + firstSeen + "\'");
    CH405BotServer.shutdown(logger.getCallerName(), "an error in parsing first seen date");
}
user.setFirstSeen(firstSeenDate);

来源数据:

isadmin = false
firstseen = 2014-12-13 06:56:27
lastseen = 2014-12-13 06:56:27
numtrades = 0

编辑: 错误日志中的字符串看起来非常好:

(ERROR 4001) Error parsing first seen date. Shutting down...
(ERROR 4001) Last seen date : '2014-12-13 06:56:27'

我需要帮助来解决这个问题。

1 个答案:

答案 0 :(得分:5)

我怀疑它是由竞争条件引起的,SimpleDateFormat不是线程安全的,如果多个线程尝试使用相同的isntance解析从String到Date的Date,它可能会搞乱内部状态那个实例

我建议使用局部变量(警告:创建此实例的成本很高),因此如果您认为它过于频繁,则可以使用FastDateFormatSimpleDateFormat的线程安全实现)或@Ray suggested switch to Java8