Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert timestamp string to long in java

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)
like image 750
Spartan Avatar asked Jul 30 '15 04:07

Spartan


People also ask

How do I convert a timestamp to a string in java?

The toString() method of the java. sql. Timestamp class returns the JDBC escape format of the time stamp of the current Timestamp object as String variable. i.e. using this method you can convert a Timestamp object to a String.

Can we convert string to long in java?

We can convert String to long in java using Long. parseLong() method.

Can we change timestamp format in java?

We can format the Timestamp value using SimpleDateFormat class. Initially, by using the Timestamp class, the time is getting displayed in a standard format, but we can format it to our own choice using SimpleDateFormat class.

Can we convert timestamp to Date in java?

Timestamp class can be converted to Date class in java using the Date class which is present in Java. Util package. The constructor of the Date class receives a long value as an argument.


1 Answers

Try this way, Example code:

java.sql.Timestamp ts2 = java.sql.Timestamp.valueOf("2015-07-24T09:45:44.000Z");
long tsTime2 = ts2.getTime();
like image 76
Bipool Avatar answered Oct 23 '22 22:10

Bipool