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)
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.
We can convert String to long in java using Long. parseLong() method.
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.
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.
Try this way, Example code:
java.sql.Timestamp ts2 = java.sql.Timestamp.valueOf("2015-07-24T09:45:44.000Z");
long tsTime2 = ts2.getTime();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With