File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes How to verify if current time is between certain time? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to verify if current time is between certain time?" Watch "How to verify if current time is between certain time?" New topic

How to verify if current time is between certain time?

bob morkos
Ranch Hand

Joined: Sep 06, 2001
Posts: 104
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

Joined: Dec 06, 2001
Posts: 3061
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!


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.

Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: How to verify if current time is between certain time?
Similar Threads
Time Difference
Time Difference
roll doesn't work properly
Need to find day of the week
Saving system date