radixk
asked on
Java Subtracting Dates
I have a small twist to the many,many questions already posted for this never ending problem.
How to find the number of minutes between two dates? The below code, does not take
the day in mind, so if end time is earlier than start time, the result is negative.
start = "2005/04/25 16:00:29";
end = "2005/04/26 13:21:15";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/dd/ mm H:mm:ss");
Date s = sdf.parse(start);
Date e = sdf.parse(end);
Calendar scal = java.util.GregorianCalenda r.getInsta nce();
scal.setTime(s);
Calendar ecal =java.util.GregorianCalend ar.getInst ance();
ecal.setTime(e);
long result = (e.getTime() - s.getTime())/60000;
result ===> -158 !
How to find the number of minutes between two dates? The below code, does not take
the day in mind, so if end time is earlier than start time, the result is negative.
start = "2005/04/25 16:00:29";
end = "2005/04/26 13:21:15";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/dd/
Date s = sdf.parse(start);
Date e = sdf.parse(end);
Calendar scal = java.util.GregorianCalenda
scal.setTime(s);
Calendar ecal =java.util.GregorianCalend
ecal.setTime(e);
long result = (e.getTime() - s.getTime())/60000;
result ===> -158 !
ASKER
start = "2005/04/25 16:00:29";
end = "2005/04/28 13:21:15";
4/28/2005 - 4/25/2005
results in the same -158
end = "2005/04/28 13:21:15";
4/28/2005 - 4/25/2005
results in the same -158
Try:
long result = ( e.getTime() / 60000L ) - ( s.getTime() / 60000L );
long result = ( e.getTime() / 60000L ) - ( s.getTime() / 60000L );
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It was a parsing problem... :-)
>> Hahaha... I know what it is:
Sorry, that was a lugh of joy, as it was doing my head in, not a laugh at anyone in particular :-)
(I read it back, and it looks a bit harsh, so I thought I'd explain)
Sorry, that was a lugh of joy, as it was doing my head in, not a laugh at anyone in particular :-)
(I read it back, and it looks a bit harsh, so I thought I'd explain)
lugh == laugh :-/
Should be
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/ dd HH:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/
Sorry Tim - notifications a bit slow ;-)
>>long result = Math.abs(result);
Can still be applied
>>long result = Math.abs(result);
Can still be applied
;-)
ASKER
Thanks much ... Four eyes are definitely better than two.
Sometimes I think It's amazing I'm able to dress myself correctly in the morning.
(Although, I'm sure my opinion of "correctly" is disputable)
As for being laughed at ... No offense taken, I find it refreshing that someone other
than myself is laughing at me.
Sometimes I think It's amazing I'm able to dress myself correctly in the morning.
(Although, I'm sure my opinion of "correctly" is disputable)
As for being laughed at ... No offense taken, I find it refreshing that someone other
than myself is laughing at me.
>> No offense taken, I find it refreshing that someone other than myself is laughing at me.
Heh, honest, it was a laugh of joy, as I'd been staring at the code too, not able to work out what was going on to give -158! ;-)
And I know what you mean about dressing... I still have to check that I have matching shoes on in the morning some days ;-)
I need to be a millionaire, and have other people sort these sorts of triviallities out for me I reckon ;-)
Good luck with it!
Tim
Heh, honest, it was a laugh of joy, as I'd been staring at the code too, not able to work out what was going on to give -158! ;-)
And I know what you mean about dressing... I still have to check that I have matching shoes on in the morning some days ;-)
I need to be a millionaire, and have other people sort these sorts of triviallities out for me I reckon ;-)
Good luck with it!
Tim
radixk, bare in mind that your calculation does not take into account day light saving time difference between date1 and date2 (if that is important to you).
long result = Math.abs(result);
?