1

The android app will notify user only between the time interval user selects.
Settings will have two timepickers to store TIME1 and TIME2

I am planning to store the time in INT format as suggested by following link
link --> Store time in int data type in sql

Following the above approach while comparing with current time:-

USE CASE 1

     TIME1          |      TIME2     |   Currenttime 
 03:00(180 INT)     | 06:00(360 INT) |  05:00(300 INT)

 TIME1 <= Currenttime <= TIME2 (USER WILL RECEIVE NOTIFICATION)


USE CASE 2   (time2 < time1)

Should i even allow user to 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 
( with respect to realtime when user wants to get notified at midnight)


 in this case user should actually get the notification if reverse time is 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 as 2 bu with more time interval)

consider same case as 2 but time as:- 

     TIME1          |      TIME2     
 15:00(900 INT)     |  10:00(600 INT)

 This means USER DOES NOT WANT NOTIFICATION form morning 10:01 to afternoon 14:59

 But user wants it for rest of the time like from afternoon 15:00 to next day morning 10:00


I think this is the best way to do , but I want to know is there any better approach ? or easier approach ? plus should user be allowed to set reverse time ? if not then user wont be able to set midnight intervals.

Help is appreciated.

Community
  • 1
  • 1
UzUmAkI_NaRuTo
  • 565
  • 3
  • 8
  • 20
  • you should store time in milliseconds. If its in millisecond your case:2 will not occur. – Janki Gadhiya Apr 19 '16 at 07:23
  • why ? what if user slelects 22:00 in timepicker 1 i.e 79200000 ms and 1 hour in timepicker2 i.e 3600000 ms. where 79200000 > 3600000 i.e case 2 of time 2 < time1. correct me if i am wrong – UzUmAkI_NaRuTo Apr 19 '16 at 07:31
  • make the selection with date+time i m saying. – Janki Gadhiya Apr 19 '16 at 07:32
  • i corrected use case a bit. actually it was time1> time2. i first considered storing date + time epoch format. but was confused alot since epoch is date specific too. am not sure how would i go about that. since, if user selects date+time1 on 19th april 3pm and date+time 2 19th april 6pm. user will get notified when current datetime is 19th april and between 3 to 6. but what about 20th april 3 to 6 ? it should be date independent right ? any guide will be helpfull thanks – UzUmAkI_NaRuTo Apr 19 '16 at 07:40

1 Answers1

0

Try below helper class, I think it's exactly what you 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 AM ==> Result:true
initialTime=9:00 AM, finalTime=12:00 PM, currentTime=11:00 AM ==> Result:true
initialTime=8:00 AM, finalTime=8:00 PM, currentTime=11:00 AM ==> 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 AM ==> Result:false
initialTime=8:00 PM, finalTime=8:00 AM, currentTime=6:00 AM ==> Result:true
Dhaval Patel
  • 10,119
  • 5
  • 43
  • 46