java - 如何检查时间是在android中只有两次晚上之间
问题描述
在我的 android 应用程序中,如果选择的时间大于 11:00 PM 和 07:00 AM,则我有条件,那么将产生额外费用。它只适用于晚上。目前,我的代码工作正常,但是当我选择一天中的时间时,情况变得真实。
这是我的代码
Calendar calendar = Calendar.getInstance();
String startDatetime = "11:00 PM";
String endDatetime = "07:00 AM";
SimpleDateFormat formatfornightcharges = new SimpleDateFormat("HH:mm aa");
int Hr24 = calendar.get(Calendar.HOUR_OF_DAY);
try {
Date startDate = formatfornightcharges.parse(startDatetime);
Date selectedTimeforBooking = formatfornightcharges.parse(time_for_night);
Date endDate = formatfornightcharges.parse(endDatetime);
if (selectedTimeforBooking.after(startDate) || selectedTimeforBooking.before(endDate)) {
Toast.makeText(context, "200", Toast.LENGTH_SHORT).show();
night_extra_charges = 200;
tv_text.setText("" + night_extra_charges);
} else {
night_extra_charges = 0;
tv_text.setText("" + night_extra_charges);
Toast.makeText(context, "0", Toast.LENGTH_SHORT).show();
}
} catch (ParseException e) {
e.printStackTrace();
}
}
解决方案
您只需要将格式化程序更改为:
SimpleDateFormat formatfornightcharges = new SimpleDateFormat("hh:mm aa", Locale.US);
而不是"hh:MM aa"
.
推荐阅读
- mediawiki - 为 Mediawiki 设置多个登录选项
- c - Is there a way to either query what would realloc do, or prevent it from copying all memory on Windows and Linux?
- javascript - How to use multiple npm auth tokens
- python - AttributeError: 'Series' object has no attribute 'startswith' when use pandas dataframe condition
- html - 如何从上到下增加彩色矩形高度的大小,[css]?
- python - kivy garden mapview方法绘制行程
- python - 如何在 Flask-JWT 中设置自定义授权响应?
- dynamics-crm - Power Automate 连接到 Dynamics 365/CRM
- java - 如何仅将对象传递给决策表的 ACTION 部分?
- kotlin - 按顺序运行 Kotlin 协程