gpt4 book ai didi

java - 我如何在android中比较两次?

转载 作者:搜寻专家 更新时间:2023-11-01 09:26:59 25 4
gpt4 key购买 nike

<分区>

我想在我的 android 应用程序中比较两次(时间戳)。这是我检查当前时间是否在两个给定时间之间的函数。

private boolean isBetween(String t1, String t2, String target){
boolean result = false;
try{

Date time1 = new SimpleDateFormat("HH:mm:ss").parse(t1);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(time1);


Date time2 = new SimpleDateFormat("HH:mm:ss").parse(t2);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(time2);
calendar2.add(Calendar.DATE, 1);


Date d = new SimpleDateFormat("HH:mm:ss").parse(target);
Calendar calendar3 = Calendar.getInstance();
calendar3.setTime(d);
calendar3.add(Calendar.DATE, 1);

Date x = calendar3.getTime();
if (x.after(calendar1.getTime()) && x.before(calendar2.getTime())) {
result = true;
//checkes whether the current time is between 14:49:00 and 20:11:13.

//Toast.makeText(UserCheckout.this, "The time is between", Toast.LENGTH_SHORT).show();
}
}
catch (ParseException e){
result = false;
Toast.makeText(UserCheckout.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
return result;
}

以下是我如何使用此功能。

if (isBetween("19:00:00", "20:00:00", currentTime)){
Toast.makeText(this, "Time is between 7 and 8", Toast.LENGTH_SHORT).show();
}

if (isBetween("20:00:00", "21:00:00", currentTime)){
Toast.makeText(this, "Time is between 8 and 9", Toast.LENGTH_SHORT).show();
}

但我得到的结果都是真的。

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