Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to verify if current time is between certain time?

 
bob morkos
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • 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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic