java - 考虑用户在 Android 应用程序中选择反向时间间隔的用例

标签 java android mysql time datetime-comparison

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

我计划按照以下链接的建议以 INT 格式存储时间
链接 --> Store time in int data type in sql

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

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


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

感谢您的帮助。

最佳答案

尝试下面的辅助类,我认为这正是您想要的。

    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

关于java - 考虑用户在 Android 应用程序中选择反向时间间隔的用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36711070/

相关文章:

java - Java 上数字递减的数字金字塔

Java:如何在 Android Studio 中生成随机信号?

android - finish() 完成 Activity 但它仍在后台

python - 远程访问 OpenShift 中托管的 MySQL DB(不寻找端口转发解决方案)

sql - 最近 7 天的 MySQL 计数数据

java - for 循环内的打印语句导致多个输出而不是一个输出,如何在不使用 "return"的情况下解决此问题?

java - 如何在 JButton 上的较大 imageIcon 上设置较小的 ImageIcon ?虽然我希望两个图像都可见

java - 未知故障(在 android.os.Binder.execTransact(Binder.java :674)) Error while Installing APKs

android - 使用 Facebook SDK 仅共享文本

MySQL SET 在第一列和第二列上的排名