gpt4 book ai didi

java - java中检查时间间隔

转载 作者:行者123 更新时间:2023-12-01 23:40:27 27 4
gpt4 key购买 nike

我想要一些 Java 代码,它可以根据当前时间告诉我学校何时开放。

当我在上午 9 点到下午 6 点之间调用此方法时,它应该返回“学校开放”,否则它应该在下午 6 点之后和上午 9 点之前返回“学校关闭”。

public Calendar shopStartTime(String msg)
{
Calendar currentTime = Calendar.getInstance();
Calendar schoolTime = Calendar.getInstance();
Calendar schoolClosedTime = Calendar.getInstance();
schoolTime.set(Calendar.HOUR_OF_DAY, 9);
schoolTime.set(Calendar.MINUTE, 0);
schoolTime.set(Calendar.SECOND, 0);
schoolTime.set(Calendar.MILLISECOND, 0);
schoolClosedTime.set(Calendar.HOUR_OF_DAY, 18);
schoolClosedTime.set(Calendar.MINUTE, 0);
schoolClosedTime.set(Calendar.SECOND, 0);
schoolClosedTime.set(Calendar.MILLISECOND, 0);
if (schoolTime.compareTo(currentTime) <= 0 &&
(currentTime.compareTo(schoolClosedTime)>=0))
{
// check for time
Toast.makeText(getSherlockActivity(), "school is closed ",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getSherlockActivity(), " school is open",
Toast.LENGTH_SHORT).show();
}
return currentTime;
}

但是代码不起作用,因为它总是返回相同的结果。如何测试其中一个时间是否位于一天中的其他两个时间之间?

最佳答案

阅读 javadoc!

if (currentTime.after(schoolTime) && currentTime.before(schoolClosedTime)) {
// school is open
} else {
// school is closed
}

关于java - java中检查时间间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18035021/

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