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.GregorianCalendar.getInstance();
scal.setTime(s);
Calendar ecal =java.util.GregorianCalendar.getInstance();
ecal.setTime(e);
                  
long result = (e.getTime() - s.getTime())/60000;


result ===>   -158 !

radixkAsked:
Who is Participating?
 
TimYatesCommented:
Hahaha... I know what it is:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/dd/mm H:mm:ss");

should be

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd H:mm:ss");
0
 
CEHJCommented:
Why not

long result = Math.abs(result);

?
0
 
radixkAuthor Commented:
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
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
TimYatesCommented:
Try:

long result = ( e.getTime() / 60000L )  - ( s.getTime() / 60000L );
0
 
TimYatesCommented:
It was a parsing problem... :-)
0
 
TimYatesCommented:
>>  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)
0
 
TimYatesCommented:
lugh == laugh :-/
0
 
CEHJCommented:
Should be

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
0
 
CEHJCommented:
Sorry Tim - notifications a bit slow ;-)

>>long result = Math.abs(result);

Can still be applied
0
 
TimYatesCommented:
;-)
0
 
radixkAuthor Commented:
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.
0
 
TimYatesCommented:
>> 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
0
 
aozarovCommented:
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).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.