如何在Android中仅在两个晚上之间检查时间

时间:2019-04-25 08:59:34

标签: java android datetime-comparison

在我的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();
                }


            }

2 个答案:

答案 0 :(得分:2)

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

var isBlack = 0;
function changeBackground() {
  if (isBlack == 0){
    var backgr = document.getElementById('bg').style="background-color:#52b0d6; color:white;";
        document.getElementById('selectback').innerHTML="CHANGE WEB BACKGROUND COLOR... (#2)";
        document.getElementById('selectback').style="background-color:#464141;";
        isBlack=true;
  isBlack= 1;
  }

  else{
    var dftBg = document.getElementById('bg').style="background-image:url('issets/images/backgrnd.jpg'); color:white;";
        document.getElementById('selectback').innerHTML="RESTORE TO DEFAULT BACKGROUND COLOR... (#3)";
        document.getElementById('selectback').style="background-color:black;";
        document.getElementById('centered').style="background-color:#464141;";
        isBlack=true;
  isBlack= 0;
  }
}

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

答案 1 :(得分:0)

试试看,它一直都在起作用,仅以这种格式更改time_for_night:-

String startDatetime = "11:00 PM";
        String endDatetime = "07:00 AM";
        String time_for_night = "11:05 PM";
        Integer night_extra_charges=0;
        SimpleDateFormat formatfornightcharges = new SimpleDateFormat("hh:mm aa", Locale.ENGLISH);
        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(ScanDocumentsActivity.this, "200", Toast.LENGTH_SHORT).show();
                night_extra_charges = 200;
                Log.wtf("night_extra_charges",""+night_extra_charges);
            } else {
                night_extra_charges = 0;
                Log.wtf("night_extra_charges",""+night_extra_charges);
                Toast.makeText(ScanDocumentsActivity.this, "0", Toast.LENGTH_SHORT).show();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }