java - considering the use case of user selecting reverse time interval in android app -
the android app notify user between time interval user selects.
settings have 2 timepickers store time1
, time2
i planning store time in int format suggested following link
link --> store time in int data type in sql
following above approach while comparing current time:-
use case 1
time1 | time2 | currenttime 03:00(180 int) | 06:00(360 int) | 05:00(300 int) time1 <= currenttime <= time2 (user receive notification)
use case 2 (time2 < time1)
should allow user select time 2
time1 | time2 | currenttime 22:00(1320 int) | 01:00(60 int) | 23:00(1380 int) time1 < currenttime > time2 1320 < 1380 > 60 22:00 < 23:00 < 01:00 ( respect realtime when user wants notified @ midnight) in case user should notification if reverse time allowed. considering user wants notification during midnight. if((currentime > time2 && currentime <= 1439)|| (currentime > 0 && currentime <= time1)){ // notify here // 1439 == 23:59 }
use case 3 (arises if use case 2 allowed/actually same 2 bu more time interval)
consider same case 2 time as:- time1 | time2 15:00(900 int) | 10:00(600 int) means user not want notification form morning 10:01 afternoon 14:59 user wants rest of time afternoon 15:00 next day morning 10:00
i think best way , want know there better approach ? or easier approach ? plus should user allowed set reverse time ? if not user wont able set midnight intervals.
help appreciated.
try below helper class, think it's want.
public class calenderutil { public static void main(string[] args) { string time_format = "hh:mm a"; string initialtime, finaltime, currenttime; initialtime = "9:00 am"; finaltime = "11:00 am"; currenttime = "10:00 am"; iscurrenttimebetweengiventime(initialtime, finaltime, currenttime, time_format); initialtime = "9:00 am"; finaltime = "12:00 pm"; currenttime = "11:00 am"; iscurrenttimebetweengiventime(initialtime, finaltime, currenttime, time_format); initialtime = "8:00 am"; finaltime = "8:00 pm"; currenttime = "11:00 am"; iscurrenttimebetweengiventime(initialtime, finaltime, currenttime, time_format); initialtime = "8:00 am"; finaltime = "8:00 pm"; currenttime = "11:00 pm"; iscurrenttimebetweengiventime(initialtime, finaltime, currenttime, time_format); initialtime = "8:00 pm"; finaltime = "8:00 am"; currenttime = "11:00 am"; iscurrenttimebetweengiventime(initialtime, finaltime, currenttime, time_format); initialtime = "8:00 pm"; finaltime = "8:00 am"; currenttime = "6:00 am"; iscurrenttimebetweengiventime(initialtime, finaltime, currenttime, time_format); } public static boolean iscurrenttimebetweengiventime(string initialtime, string finaltime, string currenttime, string timeformat) { system.out.print("initialtime=" + initialtime + ", finaltime=" + finaltime + ", currenttime=" + currenttime); boolean valid = false; string currentdate = new simpledateformat("dd-mm-yyyy", locale.english).format(new date()); try { //start time date intime = new simpledateformat("dd-mm-yyyy " + timeformat, locale.english) .parse(currentdate + " " + initialtime); calendar calendar1 = calendar.getinstance(); calendar1.settime(intime); //end time date fintime = new simpledateformat("dd-mm-yyyy " + timeformat, locale.english) .parse(currentdate + " " + finaltime); calendar calendar2 = calendar.getinstance(); calendar2.settime(fintime); date actualtime = new simpledateformat("dd-mm-yyyy " + timeformat, locale.english) .parse(currentdate + " " + currenttime); calendar calendar3 = calendar.getinstance(); calendar3.settime(actualtime); if (intime.after(fintime)) { calendar2.add(calendar.date, 1); if (actualtime.before(fintime)) { calendar3.add(calendar.date, 1); } } if ((calendar3.after(calendar1) || calendar3.equals(calendar1)) && (calendar3.before(calendar2) || calendar3.equals(calendar2))) { valid = true; } } catch (parseexception e) { e.printstacktrace(); } system.out.println(" ==> result:" + valid); return valid; } }
result:
initialtime=9:00 am, finaltime=11:00 am, currenttime=10:00 ==> result:true initialtime=9:00 am, finaltime=12:00 pm, currenttime=11:00 ==> result:true initialtime=8:00 am, finaltime=8:00 pm, currenttime=11:00 ==> result:true initialtime=8:00 am, finaltime=8:00 pm, currenttime=11:00 pm ==> result:false initialtime=8:00 pm, finaltime=8:00 am, currenttime=11:00 ==> result:false initialtime=8:00 pm, finaltime=8:00 am, currenttime=6:00 ==> result:true
Comments
Post a Comment