I have some java.sql.Time objects as follows:
String s1 = "12:00:04";
String s2 = "12:23:34";
String s3 = "12:03:02";
String s4 = "11:00:54";
int dij = j-i;
int dmj = j-m;
java.sql.Time tdi = java.sql.Time.valueOf(s1);
java.sql.Time tki = java.sql.Time.valueOf(s2);
java.sql.Time tdm = java.sql.Time.valueOf(s3);
java.sql.Time tdj = java.sql.Time.valueOf(s4);
How can I perform subtract operations of these objects? I want to get the result in java.sql.Time object itself but number of seconds will work too.
Most of the methods given in java.sql.Time class are deprecated. http://docs.oracle.com/javase/7/docs/api/java/sql/Time.html
Since
java.sql.Timewraps ajava.util.Date, I’m not sure that subtraction and putting the result in ajava.sql.Timeobject is meaningful.I would rather perform the subtraction (most likely using the milliseconds field and the getTime accessor), and store the result as a numeric representing the number of seconds/milliseconds etc.
If you’re performing a lot of arithmetic, the solution is most likely to look at Joda-Time. See the FAQ for arithmetic-related info. Joda-Time has a more consistent and useful API, plus is thread-safe (unlike the standard
java.util.Date/Calendar)