• 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

Calculate Hours Between two DateTimes

 
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to get the number of hours between two user inputs.

The following does a great job with (start)6:00 (finish)7:00 or (start)6:00 AM (finish)7:00 AM but if the user puts in something like (start)6:00 AM (finish)7:00 PM.

How do I span across am to pm times?

 
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not familiar with the DateTime class, so I can't help you there. How I'd do it though would just to call getTime() on the two Date instances. The difference of the those two values would be the time difference in milliseconds and it would just take some simple arithmetic to convert that to hours.
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ahmed Bin S wrote:



Would this still work if they input 24 hour type times?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use HH instead of hh, that allows you to use 06:00 for AM and 18:00 for PM.
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Use HH instead of hh, that allows you to use 06:00 for AM and 18:00 for PM.



I am really trying to cove the user inputting 12 hour and or 24 hour time formats
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where does the DateTime class come from? It does not appear to be in the standard Java8 API.
Use the new Java8 time classes as described in the Java™ Tutorials. Stop using Date Calendar etc.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:I am really trying to cove the user inputting 12 hour and or 24 hour time formats


My advice:
1. Don't let 'em.
2. Ask them whether they want to use 12 or 24-hour time before they start, and then enforce it.

Basically, your making life hard for yourself by wanting a program that can "guess" what they intended by what they typed.

HIH

Winston
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Winston is correct. You should be doing some validation before you call this method. I mean, what if someone enters the date as "eight thirty-two"? You can't allow that, can you? Therefore, the users have to be told which format the date should be entered, and you can therefore tell them that they must enter it in 24 hour format - there isn't anything unreasonable with that.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Where does the DateTime class come from? It does not appear to be in the standard Java8 API.
Use the new Java8 time classes as described in the Java™ Tutorials. Stop using Date Calendar etc.


I was guessing it's JodaTime. But you're right that java.time should be used. One advantage is that a single (public/private) static final DateTimeFormatter can be used, since (unlike DateFormat and SimpleDateFormat), it's thread-safe (immutable even).
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I was thinking OP had written his own date time class.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic