0

in my android app I have the condition if selected time is greater than 11:00 PM and 07:00 AM then there will be extra charges. it applicable only at night. currently, my code is work correctly but when I am selecting a time in day condition is getting true.

here is my code

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


            }
forpas
  • 160,666
  • 10
  • 38
  • 76
Dipak
  • 99
  • 1
  • 11

2 Answers2

2

You only need to change the formatter to this:

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

instead of "hh:MM aa".

forpas
  • 160,666
  • 10
  • 38
  • 76
  • but if i have added && it not true even i am selecting night time – Dipak Apr 25 '19 at 09:16
  • You must use `&&` because you want the time to be between `startDate` and `endDate`. If you use `||` then all times return `true` because all times are either after `startDate` or before `endDate`. If still you don't get the result you want, you must debug to find the problem. What is the value of `time_for_night` that fails? Is it parsed correctly to `selectedTimeforBooking`? – forpas Apr 25 '19 at 09:22
  • Startdate Thu Jan 01 11:00:00 GMT+05:30 1970 selectedTimeforBooking = Thu Jan 01 11:55:00 GMT+05:30 1970 end time=Thu Jan 01 07:00:00 GMT+05:30 1970 in this case condition should true but it goes in else condition – Dipak Apr 25 '19 at 09:28
  • End time should be end time=Wed Jan 02 07:00:00 GMT+05:30 1970 NOT end time=Thu Jan 01 07:00:00 GMT+05:30 1970 – pa1.Shetty Apr 25 '19 at 09:35
  • See my edited answer. The times were not formatted correctly. – forpas Apr 25 '19 at 09:36
  • your answer is working for only today but when i am selecting time on tomorrow morning it will goes in else condition – Dipak Apr 25 '19 at 09:59
  • startDate=Thu Jan 01 23:00:00 GMT+05:30 1970 selectedTimeforBooking=Thu Jan 01 04:30:00 GMT+05:30 1970 endDate=Fri Jan 02 07:00:00 GMT+05:30 1970 – Dipak Apr 25 '19 at 10:03
  • You store only the time in the date variables. This is why you get all these inconsistencies. Check my edited answer. – forpas Apr 25 '19 at 10:15
0

Try this ,it is working for all time, only change time_for_night in this format :-

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();
        }
Ankit Gupta
  • 64
  • 10