gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:20:37 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com