Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird Date parsing error in Java

While parsing a large number of dates in java, I occasionally get this weird bug :

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)

I this case, the date was

2014-12-13 06:56:27

The date format was

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

My code:

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

Source data :

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

EDIT : The string in the error log seems perfectly fine :

(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.

like image 932
Hele Avatar asked Dec 28 '14 08:12

Hele


People also ask

What is date parsing in Java?

parse ( "2009-12-31" ); The string passed as parameter to the SimpleDateFormat class is a pattern that tells how the instance is to parse and format dates. In the example above I used the pattern "yyyy-MM-dd" which means 4 digits for the year (yyyy), two digits for month (MM) and two digits for day(dd).

What is parse error in Java?

The Java error message Reached End of File While Parsing results if a closing curly bracket for a block of code (e.g, method, class) is missing. The fix is easy — just proofread your code.

How do you format a date in Java?

SimpleDateFormat can be created using the SimpleDateFormat constructor. The constructor is a parametrised constructor and needs a String pattern as the parameter. The String pattern is the pattern which will be used to format a date and the output will be generated in that pattern as “ MM-dd-yyyy ”.


1 Answers

I suspect it is caused by race condition, SimpleDateFormat is not thread-safe and if multiple threads tries to parse Date from String to Date using same isntance it could mess up internal state of that instance

I would suggest either using local variable (warn: it is expensive to create this instance), so if you think it is too frequent, you can use FastDateFormat (thread-safe implementation of SimpleDateFormat) or as @Ray suggested switch to Java8

like image 106
jmj Avatar answered Oct 05 '22 07:10

jmj