• 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

How to verify if current time is between certain time?

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to create a function to calculate current time (for e.g.: 10:00 am) is between (07:00 am - 6:00 pm) > return a boolean true or false
Any help would be appreciated.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, how is the time represented? Are you using the Date class? If so, you should take a look at the Calendar class which provides methods to obtain the hour and minutes from a Date. Calendar also provides before() and after() methods that you might find useful.

If you aren't using Date, then I would guess that you just have a String. Do you know what format this String uses? How can you obtain the hour and minutes (and seconds?) from the String as ints so you can compare them? While it is a good learning exercise to do this on your own, the Java API provides the DateFormat class that converts a date represented as a String into a Date object and vice-versa. Most likely you will use the SimpleDateFormat class to specify your own custom format for the time. Once you convert the String into a Date, you can easily use Calendar as described above to finish doing the comparison.

I hope this helps. Let us know what happens and come back with more questions if you get stuck.

Keep Coding!

Layne

p.s. The links above are all for the Java 1.5 API. If you are using an older version of Java, you can find links for the documentation here. In fact, I suggest that you visit this link anyway and bookmark the documentation for the version of Java you are using. It is an invaluable tool when you need to look up a particular class or method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic