首页 > 解决方案 > 如何检查时间是在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();
                }


            }

标签: javaandroiddatetime-comparison

解决方案


您只需要将格式化程序更改为:

SimpleDateFormat formatfornightcharges = new SimpleDateFormat("hh:mm aa", Locale.US);

而不是"hh:MM aa".


推荐阅读