This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes find time differnce between to calender Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "find time differnce between to calender" Watch "find time differnce between to calender" New topic
Author

find time differnce between to calender

Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
hi guys iam trying to find differnce between two calender instances this is my code

iam not able to get proper results



actually i need to 3.5 as output but i am getting 15.5

please tell me where iam wrong
looking for your replies
thanks
amir
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38517
    
  23
From 8.00am 10th July to 11.30pm 10th July is 15½ hours. Look very closely at the times you are setting.
Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
Campbell Ritchie wrote:From 8.00am 10th July to 11.30pm 10th July is 15½ hours. Look very closely at the times you are setting.


eventhough i changed the following lines



iam getting 16.5 as output

iam thinking that from 8.00am to 12.30 pm the differnce is 4.5 hrs iam getting execess 12 hrs

that is my question
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Amirtharaj Chinnaraj wrote:
Campbell Ritchie wrote:From 8.00am 10th July to 11.30pm 10th July is 15½ hours. Look very closely at the times you are setting.


eventhough i changed the following lines



iam getting 16.5 as output

iam thinking that from 8.00am to 12.30 pm the differnce is 4.5 hrs iam getting execess 12 hrs

that is my question


You have the following line:



What does that line do?


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
the line

is to set wether the time is for before or after Noon







W. Joe Smith wrote:
Amirtharaj Chinnaraj wrote:
Campbell Ritchie wrote:From 8.00am 10th July to 11.30pm 10th July is 15½ hours. Look very closely at the times you are setting.


eventhough i changed the following lines



iam getting 16.5 as output

iam thinking that from 8.00am to 12.30 pm the differnce is 4.5 hrs iam getting execess 12 hrs

that is my question


You have the following line:



What does that line do?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38517
    
  23
8.00am 10th July 10 12.00pm (midnight) 10th July is 16 hours. Add 30 minutes and you get 16½ hours. You need to check the Calendar class documentation carefully about that.

Or look elsewhere for a Calendar class anybody can actually understand
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Amirtharaj Chinnaraj wrote:the line

is to set wether the time is for before or after Noon


So, if you are setting the 11:30 to PM, and the first one is set to 8:00 AM, how many hours would be in between?

I think if you look at that you can see why you are getting 15.5 or 16.5 instead of the 3.5 or 4.5 you believe you should be getting.
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
8am is 0800; 1130pm is 2330 - the difference is 15.5hours, as your code shows. Are you sure you want the answer 3.5? Because that's not correct when you are concerned about AM and PM.

If you don't care about AM/PM, then I suggest you just either make sure both calendar instances are set to AM (or PM, doesn't matter so long as they are the same) or do basic arithmetic on the hours and minutes portions of your Calendar instance, rather than rely of milliseconds from the epoch. e.g.

Be careful not to use Calendar.HOUR_OF_DAY as that is based on the 24 hour clock and you will get the answer 15.5 again.

J.


SCJP6
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: find time differnce between to calender