java - Compare two times in Android -
i know quite discussed topic think have specific problem.
i'm storing opening , closing time of stores on database gps coordinates. i'd able show stores on map green marker if store open , red if close (wrt current time).
my problem i'm using method string.compareto(string) know if store closed or open. let's on monday, store close @ 02:00 in morning (tuesday morning), current time 23:00. how can tell store still open since 02:00 < 23:00 ?
thank answer,
arnaud
edit: i'm editing answer give better idea of meant. also, try second , re-think way store data structure. important part of programming , in cases can difference between bad design , design implementation.
storing time string not idea, in case should (i think) this: (this code assumes hours store opening , closing time refer same day now
)
// current time. calendar = calendar.getinstance(); // create opening time. calendar openingtime = calendar.getinstance(); // set opening hours. (important: useful know day also). openingtime.set(calendar.hour_of_day, storeopeningtime); // (storeopeningtime in 24-hours format 10pm use 22). // check store "was opened" today. if (now.before(openingtime)) { return closed; } // if store "was opened" today check it's not closed ("tricky" part). // create closing time , closing time store. calendar closingtime = calendar.getinstance(); // check if in part. if (storeclosingtime < 12 // closing time in part. && storeclosingtime < openingtime // closing time before opening time, meaning next day. // , closingtime same day (edge case if passed midnight when getting closingtime) && closingtime.get(calendar.day_of_week) == now.get(calendar.day_of_week)) { // closing time next day. closingtime.add(calendar.day_of_week, 1); } // set closing hours. closingtime.set(calendar.hour_of_day, storeclosingtime); // (storeclosingtime in 24-hours format 10pm use 22). // check if store not closed yet. if (now.before(closingtime)) { return open; } // store closed. return closed;
i haven't tested this, think according questions , how save opening , closing times, should work.
important: there more edge cases , tweaks can make code improve it, don't have enough time now. wanted give general guiding hand solve problem. handling time , dates never fun, need keep in mind lot of elements time zones , clock shifting in winter , summer in different countries. not finished implementation in way, , getting finished state not simple.
Comments
Post a Comment