-2

I want to calculate the difference in minutes of two time stamp variables(endTime_check and endtime) .. first variable endTime_check is declared as String and Second variable endtime is taken from database type is DateTime.

code is :

DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
String endTime_check = "";
endTime_check = timeFormat.format(cal.getTime());    
//endtime is taken from database, type is DateTime
endtime = rs.getString("endtime");
earthmover
  • 4,395
  • 10
  • 43
  • 74
malik
  • 1
  • 1
  • 3
  • You want to do the calculation in Java or MySQL? – Rahul Jun 05 '14 at 11:57
  • Have a look here https://stackoverflow.com/questions/625433/how-to-convert-milliseconds-to-x-mins-x-seconds-in-java and here https://stackoverflow.com/questions/15540801/time-difference-program/15541322#15541322 (assuming you want to calculate the difference in java) – Dave Jun 05 '14 at 12:00
  • 1
    @OP: There is no meaning in finding a time difference unless you know the dates. – Ravinder Reddy Jun 05 '14 at 12:07
  • [*This answer*](http://stackoverflow.com/a/19887135/767881) should help. – Ravinder Reddy Jun 05 '14 at 12:08
  • possible duplicate of [How to find the duration of difference between two dates in java?](http://stackoverflow.com/questions/17940200/how-to-find-the-duration-of-difference-between-two-dates-in-java) – Basil Bourque Jun 05 '14 at 18:17

1 Answers1

0

Something like this:

Date endTime = rs.getDate("endtime");
String endTimeCheck = "12:34:56";

DateFormat df = new SimpleDateFormat("HH:mm:ss");
long endTimeCheckMillis = df.parse(endTime).getTime();
long endTimeMillis = endTime.getTime();
long differenceMinutes = Math.abs(((endTimeCheckMillis - endTimeMillis) / 60000));

If you want a signed difference leave out the 'Math.abs' call.

Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60