Shubham Garg
Shubham Garg

Reputation: 67

Error in converting Date to String

        Date date = new Date();
        try {
            date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(s);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Timestamp timestamp=new Timestamp(date.getTime());
        id=Long.parseLong(timestamp.toString());

On executing the above code I got this error:

java.lang.NumberFormatException: For input string: "2018-08-29 16:35:31.753"
    at 
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

What's wrong in it now?

Upvotes: 0

Views: 961

Answers (1)

xingbin
xingbin

Reputation: 28279

The error happens when you try to get long value by Timestamp.toString(), since it returns 2018-08-29 16:35:31.753, which is not a valid long.

Just use:

long id = timestamp.getTime();

Upvotes: 2

Related Questions