Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

find time differnce between to calender

 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49457
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49457
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic