1

android 应用程序只会在用户选择的时间间隔之间通知用户。
设置将有两个时间选择器来存储TIME1TIME2

我打算按照以下链接
链接的建议以 INT 格式存储时间->在 sql 中以 int 数据类型存储时间

在与当前时间比较时遵循上述方法:-

用例 1

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

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


用例 2(时间 2 < 时间 1)

我是否应该允许用户选择时间 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
}


用例 3(如果用例 2 允许/实际上与 2 bu 相同且时间间隔更长,则出现)

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


我认为这是最好的方法,但我想知道有没有更好的方法?或更简单的方法?另外应该允许用户设置反向时间吗?如果没有,那么用户将无法设置午夜间隔。

帮助表示赞赏。

4

1 回答 1

0

试试下面的助手类,我认为这正是你想要的。

    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;
    }
}

结果:

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
于 2016-04-19T07:35:31.427 回答