Convert timestamp string to long in java
Try this way, Example code:
java.sql.Timestamp ts2 = java.sql.Timestamp.valueOf("2015-07-24T09:45:44.000Z");
long tsTime2 = ts2.getTime();
The simplest solution would be to use Java 8's Date/Time API
LocalDateTime from = LocalDateTime.parse("2015-07-24T09:39:14.000Z", DateTimeFormatter.ISO_DATE_TIME);
LocalDateTime to = LocalDateTime.parse("2015-07-24T09:45:44.000Z", DateTimeFormatter.ISO_DATE_TIME);
System.out.println(from + " - " + to);
LocalTime fromTime = from.toLocalTime();
LocalTime toTime = to.toLocalTime();
System.out.println(fromTime + " - " + toTime);
System.out.println(fromTime + " before " + toTime + " = " + fromTime.isBefore(toTime));
System.out.println(fromTime + " after " + toTime + " = " + fromTime.isAfter(toTime));
System.out.println(fromTime + " equals " + toTime + " = " + fromTime.equals(toTime));
System.out.println(fromTime + " compareTo " + toTime + " = " + fromTime.compareTo(toTime));
Which outputs
2015-07-24T09:39:14 - 2015-07-24T09:45:44
09:39:14 - 09:45:44
09:39:14 before 09:45:44 = true
09:39:14 after 09:45:44 = false
09:39:14 equals 09:45:44 = false
09:39:14 compareTo 09:45:44 = -1
If you're not using Java 8, then use Joda-Time which works in similar way
Joda-Time example...
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
import org.joda.time.format.ISODateTimeFormat;
public class JodaTimeTest {
public static void main(String[] args) {
LocalDateTime from = LocalDateTime.parse("2015-07-24T09:39:14.000Z", ISODateTimeFormat.dateTime());
LocalDateTime to = LocalDateTime.parse("2015-07-24T09:45:44.000Z", ISODateTimeFormat.dateTime());
LocalTime fromTime = from.toLocalTime();
LocalTime toTime = to.toLocalTime();
System.out.println(fromTime + " - " + toTime);
System.out.println(fromTime + " before " + toTime + " = " + fromTime.isBefore(toTime));
System.out.println(fromTime + " after " + toTime + " = " + fromTime.isAfter(toTime));
System.out.println(fromTime + " equals " + toTime + " = " + fromTime.equals(toTime));
System.out.println(fromTime + " compareTo " + toTime + " = " + fromTime.compareTo(toTime));
}
}
Which outputs
09:39:14.000 - 09:45:44.000
09:39:14.000 before 09:45:44.000 = true
09:39:14.000 after 09:45:44.000 = false
09:39:14.000 equals 09:45:44.000 = false
09:39:14.000 compareTo 09:45:44.000 = -1
Another option is by using SimpleDateFormat (May not be the best compare to JODA Time)
public static void main(String[] args) throws ParseException {
String st1 = "2015-07-24T09:39:14.000Z";
String st2 = "2015-07-24T09:45:44.000Z";
String time1 = st1.substring(st1.indexOf("T") + 1, st1.indexOf(".0"));
String time2 = st2.substring(st2.indexOf("T") + 1, st2.indexOf(".0"));
Date dateTime1 = new java.text.SimpleDateFormat("HH:mm").parse(time1);
Date dateTime2 = new java.text.SimpleDateFormat("HH:mm").parse(time2);
System.out.println(dateTime1.after(dateTime2));
}