Java parsing date error

When analyzing a large number of dates in java, I sometimes get this strange error:

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) 

In this case, the date was

 2014-12-13 06:56:27 

Date format was

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

My code is:

 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); 

Initial data:

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

EDIT: A line in the error log looks great:

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

I need help on how to solve this problem.

+5
source share
1 answer

I suspect this is caused by a race condition, SimpleDateFormat not thread safe, and if multiple threads try to parse Date from String to Date using the same isntance, this can ruin the internal state of this instance

I would suggest either using a local variable (caution: creating this instance is expensive), so if you think this is too common, you can use FastDateFormat (a thread-safe implementation of SimpleDateFormat ) or as the @Ray proposed switch to Java8

+4
source

Source: https://habr.com/ru/post/1209980/


All Articles