0

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

2 Answers2

0

According to the Java API, the AM/PM format marker should be the lowercase letter a, not k, if this is the format of the arguments you're passing to your method. This code should work for you:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");

        checkTimings("02:00 PM", "02:00 PM");
    }

    public static boolean checkTimings(String startTime, String endTime) {
        String pattern = "hh:mm a";
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.US);
        try {
            Date date1 = sdf.parse(startTime);
            Date date2 = sdf.parse(endTime);
            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;
    }
}

Additionally, you could polish the conditional logic to check if the values passed as arguments are coetaneous:

public static boolean checkTimings(String startTime, String endTime) {
    String pattern = "hh:mm a";
    SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.US);
    try {
        Date date1 = sdf.parse(startTime);
        Date date2 = sdf.parse(endTime);
        int compareResult = date1.compareTo(date2);
        if (compareResult < 0) {
            System.out.println("time1 is before time2");
            return true;
        } else if (compareResult == 0) {
            System.out.println("time1 is equal to time2");
            return true;
        } else {
            System.out.println("time1 is after time2");
            return false;
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return false;
}
alejandroMAD
  • 251
  • 2
  • 13
0

Don't use SimpleDateFormat as it's outdated
Use java.time classes to format the data.

// parse you date using DateTimeFormatter
val time1 = LocalTime.parse("02:30 AM", DateTimeFormatter.ofPattern("hh:mm a"))
val time2 = LocalTime.parse("02:30 PM", DateTimeFormatter.ofPattern("hh:mm a"))
// compare time using isAfter or isBefore
val isLate = time1.isAfter(time2)
Nitish
  • 3,075
  • 3
  • 13
  • 28