【发布时间】:2020-05-04 12:13:33
【问题描述】:
示例:我的当前时间 = 晚上 8:25,这意味着当前时间在晚上 7:00 到上午 10:00 之间。那么如何确定它&如果里面显示一条消息?
这是餐厅时间限制。从晚上 7:00 到上午 10:00 时间范围内用户不能订购任何东西。
try {
// Start Time
String string1 = "07:00 PM";
Date time1 = new SimpleDateFormat("hh:mm a").parse(string1);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(time1);
calendar1.add(Calendar.DATE, 1);
// End Time
String string2 = "10:00 AM";
Date time2 = new SimpleDateFormat("hh:mm a").parse(string2);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(time2);
calendar2.add(Calendar.DATE, 1);
// Get Current Time
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
String currenttime = sdf.format(date);
Date d = new SimpleDateFormat("hh:mm a").parse(currenttime);
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())) {
System.out.println("Not possible to order now");
}
else
{
System.out.println("YES POSSIBLE");
}
} catch (ParseException e) {
e.printStackTrace();
}
【问题讨论】:
-
你试过任何代码吗?
-
请出示您的代码。你有什么输入时间的数据类型?
-
您的代码(虽然需要一些改进)似乎可以按预期运行,您是否遇到任何错误?
-
您使用的是哪个 API 级别?
-
顺便考虑扔掉长期过时且臭名昭著的麻烦
SimpleDateFormat和朋友,并将ThreeTenABP添加到您的Android项目中,以便使用java.time,现代Java日期和时间API .使用起来感觉好多了。