0

I am getting UTC date from db. And subtracting that from current utc DateTime. I would like to have diff. in minutes. Something in not quite right.

            Date currenttimedate = DateTime.UTCNow();
            String lastsynctime = c.getString(c.getColumnIndex(columnname)).toString();
            Date lastruntimedate = DateTime.StringDateToDate(lastsynctime, "MM/dd/yyyy HH:mm:ss");
            long diffInMs = (currenttimedate. getTime() - lastruntimedate.getTime());

            // convert it to Minutes
            long diffInMins = TimeUnit.MILLISECONDS.toMinutes(diffInMs);

            // return time in minutes
            response = (int)(diffInMins);

It's returning something like 720 minutes. Expected answer can not be more than 5. As data is synced every 5 mins.

user533844
  • 2,053
  • 7
  • 36
  • 43
  • 1
    add some debug lines to ensure your currenttimedate and lastruntimedate are what you think they are. ie, is the timezone throwing things off? – invertigo Feb 26 '13 at 18:40
  • have you looked at the times returned to ensure that the diff should be about 5mins? – Hassan Voyeau Feb 26 '13 at 18:40
  • Yes. Date from db is 02/26/2013 06:45:29 PM. And UTCNow is Tue Feb 26 18:45:36 CST 2013. So the diff. should be 0 mins. It's showing 720 mins. – user533844 Feb 26 '13 at 18:48
  • And getTime() is returning 1361926090000 1361882832000 – user533844 Feb 26 '13 at 18:50
  • The difference between the two times you posted is 720 minutes(0645->1845). Make sure you're using UTCNow for both times, or at least make sure they're in the same time zone. – Geobits Feb 26 '13 at 18:53
  • I checked it. Is it something related to AM/PM format and 24 hr. format ? – user533844 Feb 26 '13 at 19:37
  • Answer is http://stackoverflow.com/questions/5351483/calculate-date-time-difference-in-java – user533844 Feb 26 '13 at 19:59

0 Answers0