Convert timestamp string to long in java

Thanga picture Thanga · Jul 30, 2015 · Viewed 39.2k times · Source

I have to fetch time stamp from DB and retrieve only time and compare two time.

//below are the string values

 String st1 = "2015-07-24T09:39:14.000Z";      
 String st2 = "2015-07-24T09:45:44.000Z";

//retrieving only time 09:39:14

 String s = st1.substring(st1.indexOf("T") + 1, st1.indexOf(".0"));

//string to Long.

 Long time = Long.parseLong(s);

 Long tim1=Long.valueOf(s).longValue();

Error:

java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Long.parseLong(Unknown Source)
    at java.lang.Long.parseLong(Unknown Source)

Answer

Bipool picture Bipool · Jul 30, 2015

Try this way, Example code:

java.sql.Timestamp ts2 = java.sql.Timestamp.valueOf("2015-07-24T09:45:44.000Z");
long tsTime2 = ts2.getTime();