Johan
Johan

Reputation: 35194

Minutes between 2 dates

obj.date is a unix timestamp(long). I've checked that the date is correct, but I can't manage to get the difference in minutes between my two date objects. For example, I get 4, when I'm expecting something close to 30. Why is that?

Date now = new Date();
Date then = new Date((long)obj.date*1000);

int secondsbetweendates = (int) ((then.getTime()-now.getTime())/1000);

int minutesbetweendates = (secondsbetweendates/1000) % 60;

Upvotes: 1

Views: 103

Answers (1)

Mark Byers
Mark Byers

Reputation: 838186

To convert seconds to minutes you can divide by 60:

int minutesbetweendates = secondsbetweendates / 60;

Note that this returns an integer and truncates the result, meaning that only complete minutes will be counted. In other words 119 seconds will become 1 minute, not 2.

You should also be aware that it can also return a number greater than 60 if the time difference is more than one hour.

Upvotes: 3

Related Questions