I have two time value as string, I need to check the start time is before end time. The start and end times are 02:00 AM and 02:00 PM. I have used the following code. But it gives me false. I have tried with HH:mm k, HH:mm a and HH:mm aa patterns, all result in false. It should give true, since 2 am is before 2pm, right?
public static boolean checkTimings(String startTime, String endTime) {
String pattern = "HH:mm k";
SimpleDateFormat sdf = new SimpleDateFormat(pattern,Locale.US);
try {
Date date1 = sdf.parse(startTime);
Date date2 = sdf.parse(endTime);
Log.e("date 1 ", date1.toString());
Log.e("date 2 ", date2.toString());
if (date1.before(date2)) {
System.out.println("time1 is before time2");
return true;
} else {
System.out.println("time1 is after time2");
return false;
}
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}