Reputation: 178
I know this is a quite discussed topic but I think I have a very specific problem.
I'm storing opening and closing time of stores on a database as well as GPS coordinates. I'd like to be able to show the stores on a map with a green marker if the store is open and red if close (wrt current time).
My problem is that I'm using the method string.compareTo(string) to know if the store is closed or open. Let's say we are on monday, the store close at 02:00 in the morning (tuesday morning), current time is 23:00. How can I tell the store is still open since 02:00 < 23:00 ?
Thank you for any answer,
Arnaud
Upvotes: 0
Views: 2109
Reputation: 12015
edit: I'm editing my answer to give a better idea of what I meant. Also, try to back up a second and re-think the way you store your data structure. This is a very important part of programming and in most cases can be the difference between a bad design and a good design implementation.
Storing the time as a string is not a good idea, but in your case what you should do (I think) is this: (This code assumes that the hours for the store opening and closing time refer to the same day as now
)
// Get the current time.
Calendar now = Calendar.getInstance();
// Create the opening time.
Calendar openingTime = Calendar.getInstance();
// Set the opening hours. (IMPORTANT: It will be very useful to know the day also).
openingTime.set(Calendar.HOUR_OF_DAY, storeOpeningTime); // (storeOpeningTime is in 24-hours format so for 10PM use 22).
// Check that the store "was opened" today.
if (now.before(openingTime)) {
return CLOSED;
}
// If the store "was opened" today check that it's not closed already ("tricky" part).
// Create the closing time and closing time for the store.
Calendar closingTime = Calendar.getInstance();
// Check if we are in the AM part.
if (storeClosingTime < 12 // Closing time is in the AM part.
&& storeClosingTime < openingTime // Closing time is before opening time, meaning next day.
// now and closingTime is the same day (edge case for if we passed midnight when getting closingTime)
&& closingTime.get(Calendar.DAY_OF_WEEK) == now.get(Calendar.DAY_OF_WEEK)) {
// Closing time is next day.
closingTime.add(Calendar.DAY_OF_WEEK, 1);
}
// Set the closing hours.
closingTime.set(Calendar.HOUR_OF_DAY, storeClosingTime); // (storeClosingTime is in 24-hours format so for 10PM use 22).
// Check if the store is not closed yet.
if (now.before(closingTime)) {
return OPEN;
}
// Store is closed.
return CLOSED;
I haven't tested this, but I think according to your questions and how to save the opening and closing times, this should work.
Important: There are more edge cases and tweaks you can make to the code to improve it, but I don't have enough time to do it now. I wanted to give a general guiding hand to solve your problem. Handling time and dates is never fun, you need to keep in mind a lot of elements like time zones and clock shifting in winter and summer in different countries. This is not a finished implementation in any way, and getting it to a finished state is not simple.
Upvotes: 3