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