• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Converting float value to Time

 
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What did you try, and what problems did you face?

How would you do this without a computer?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 209
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

What happens if you subtract float value 8.45 from float value 10.30?

:)
 
kittu shusma
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would be good to have the code in a utility class and also have JUnit tests around it for testing purposes,

Cheers,

Niall
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :)

Cheers,

Niall
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kittu shusma wrote: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....



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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And how did you do it? It would be interesting to see how you solved that problem.
reply
    Bookmark Topic Watch Topic
  • New Topic