1

I need to know the difference between two java.sql.Time objects and the difference must be measured in minutes. This is how I do it:

java.sql.Time srTime = sr.getTime(); // 12:40:04
java.sql.Time chTime = ch.getTime(); // 12:32:00
long diff = Math.abs(srTime.getTime() - chTime.getTime()); // 484000

The result (diff) is equal to 484000. How to make it equal to 8 minutes?

Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217

3 Answers3

4

Use division

int result =   484000
             / 1000   /* take out milliseconds */ 
             / 60     /* convert to minutes    */;
System.out.println(result);

Output: 8

2

You can simply divide it like this:

long diff = Math.abs(srTime.getTime() - chTime.getTime());
long res = diff/60000;

The diff which you are getting is in milliseconds, so you need to divide it by 60000 to get that in minutes.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2

If you are using java 1.7 you can use the TimeUnit object. http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html

Once you are getting a response um milliseconds, look at this example

long minutes = TimeUnit.MILLISECONDS.toMinutes(diff);
long seconds = TimeUnit.MILLISECONDS.toSeconds(diff);
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331