【问题标题】:considering the use case of user selecting reverse time interval in android app考虑用户在 android 应用中选择反向时间间隔的用例
【发布时间】:2016-08-11 04:47:45
【问题描述】:

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

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

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

用例 1

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

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


用例 2   (time2

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


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

感谢您的帮助。

【问题讨论】:

  • 您应该以毫秒为单位存储时间。如果它以毫秒为单位,您的 case:2 将不会发生。
  • 为什么?如果用户在时间选择器 1 中选择 22:00,即 79200000 毫秒,在时间选择器 2 中选择 1 小时,即 3600000 毫秒,该怎么办?其中 79200000 > 3600000 即时间 2
  • 用我说的日期+时间进行选择。
  • 我稍微更正了用例。实际上是时间1>时间2。我首先考虑存储日期+时间纪元格式。但是很困惑,因为时代也是特定于日期的。我不知道我会怎么做。因为,如果用户在 4 月 19 日下午 3 点选择日期 + 时间 1,在 4 月 19 日下午 6 点选择日期 + 时间 2。当当前日期时间是 4 月 19 日和 3 到 6 之间时,用户会收到通知。但是 4 月 20 日 3 到 6 呢?它应该是独立于日期的吧?任何指南都会有所帮助,谢谢

标签: java android mysql time datetime-comparison


【解决方案1】:

试试下面的辅助类,我想这正是你想要的。

    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

【讨论】:

  • 这肯定有帮助!谢谢,我会试试的。一个疑问,将这种时间格式存储在 sql 中的适当数据类型是什么?
  • 非常感谢 :) 这也应该适用于 24 小时格式,对吧? "HH:mm"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-19
  • 1970-01-01
  • 2020-10-04
  • 1970-01-01
  • 2022-06-15
  • 2012-07-05
  • 2021-03-15
相关资源
最近更新 更多