?
Solved

Java Subtracting Dates

Posted on 2005-04-28
13
Medium Priority
?
9,732 Views
Last Modified: 2012-06-27
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 !

0
Comment
Question by:radixk
  • 7
  • 3
  • 2
  • +1
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13885728
Why not

long result = Math.abs(result);

?
0
 

Author Comment

by:radixk
ID: 13885790
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13885826
Try:

long result = ( e.getTime() / 60000L )  - ( s.getTime() / 60000L );
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Accepted Solution

by:
TimYates earned 500 total points
ID: 13885869
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13885906
It was a parsing problem... :-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13885914
>>  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
 
LVL 35

Expert Comment

by:TimYates
ID: 13885917
lugh == laugh :-/
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13885927
Should be

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13885947
Sorry Tim - notifications a bit slow ;-)

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

Can still be applied
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13885950
;-)
0
 

Author Comment

by:radixk
ID: 13886037
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13886091
>> 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
 
LVL 15

Expert Comment

by:aozarov
ID: 13888358
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question