kittu shusma wrote:Hi,
i want to subtract two time or float values like(9.30-8.30=1)and i want to convert the result into minutes format.
please explain me the process.
In addition to what DB said, I will point out that if you're using a float value of 9.30 to represent the time 9:30, then you're doing it wrong. I'll let you figure out why.
Niall Loughnane
Ranch Hand
Joined: Dec 07, 2006
Posts: 200
posted
1
Hi Kittu,
The best thing to do when you come across a challenge that is puzzling is to write out on a text file what you are trying to do,
Then convert that english into methods and hence a class,
Break the overall task into smaller tasks and even smaller smaller tasks and you will come across your solution :)
Hope this helps :)
Niall
kittu shusma
Greenhorn
Joined: Apr 18, 2012
Posts: 14
posted
0
Hi,
i have school time in float value like:8.30,9.30....
when i subtract 9.30-8.30 i got 1..
(9.30-8.30 >= 1) like..
10.45-10.30 < 0.15 we know,
in that how to write 0.15 value in time format when compare those values.
by using java..
i hope you all are understand..please any one help me to resolve this issue.
Niall Loughnane
Ranch Hand
Joined: Dec 07, 2006
Posts: 200
posted
0
Hi,
What happens if you subtract float value 8.45 from float value 10.30?
:)
kittu shusma
Greenhorn
Joined: Apr 18, 2012
Posts: 14
posted
0
Hi,
it allocates given data wrongly..
please help me how to calculate 0.15 is time format in 15 min..and then
when i compare 12.45 - 1.30 i got -11 value..
how to convert -11 value in times format give to me example....
Niall Loughnane
Ranch Hand
Joined: Dec 07, 2006
Posts: 200
posted
0
Hi Kittu,
the data isnt calculated wrongly,
float values (as are int's, double's and any number) is calculated in base 100,
time is in base 60,
the task could be completed using:
* java.util.time
* JodaDateTime
* or create the code ourselves,
Here is the code that does the task, But PLEASE try and understand what it is doing rather than copy and paste :)
[Removed the solution - Martin Vajsar]
Hope this helps,
Cheers,
Niall
Niall Loughnane
Ranch Hand
Joined: Dec 07, 2006
Posts: 200
posted
0
It would be good to have the code in a utility class and also have JUnit tests around it for testing purposes,
I would try to get rid of the floats. They bring in the floating point arithmetic which is fraught with caveats.
How about representing your times as number of minutes or seconds since midnight? That way you would deal only with integers, and would not have to solve the am/pm transition in subtracting. You would need to write methods to convert the internal numbers to/from the text representation, of course, and use the text representation in the presentation layer of your application.
Niall, thanks for your replies. However, keep in mind that people learn best when they try to figure out the solution themselves. I believe that in this situation the pointers you've given will serve the original poster much better than a full code you've also included. I've therefore removed the code from your post. Please see DontBeACodeMill (<-- click this link) for further information.
Niall Loughnane
Ranch Hand
Joined: Dec 07, 2006
Posts: 200
posted
0
Hi Martin,
Cool and I agree with you 1000%,
It is better for the coder to discover the solution themselves but Kitsu seems to be a little bit distraught :)
it allocates given data wrongly..
please help me how to calculate 0.15 is time format in 15 min..and then
when i compare 12.45 - 1.30 i got -11 value..
how to convert -11 value in times format give to me example....
I agree with Martin that you shouldn't be using float. However, if you insist on using it, think about what 9:30 really means, and what 9.30 really means.
9:30 is 9 1/2 hours past midnight (or past noon). It's halfway between 9:00 and 10:00.
9.30 is 0.3 (30%) of the way between 9.00 and 10.00.
Your issue here is not a Java question at all. It's just simple arithmetic. I'm sure you can figure it out yourself, if you put your mind to it. Here's a hint: What's 30 / 60, in decimal notation?
kittu shusma
Greenhorn
Joined: Apr 18, 2012
Posts: 14
posted
0
Hi Everybody,
Thanks all for giving reply,
i got solution...
i did not change any value i just wrote an small utility that's it...
Thanks,
Kittushusma
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
4
posted
0
And how did you do it? It would be interesting to see how you solved that problem.