• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

find time differnce between to calender

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 75629
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 75629
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Hey! You're stepping on my hand! Help me tiny ad!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic