How to check if a timestamp is late

I have a timestamp and I want to check whether the time is before or after a certain time (9:00 AM). To be more specific, I want to know whether a person is late or not and the cut-off time is 9 AM. How do I code this?

2015-09-22 08:59:59 //Print on time
2015-09-22 09:00:00 //Print on time
2015-09-22 09:00:01 //Print you are late

Answer

I think you can do like:

public static final String TIME_STAMP_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static void main(String[] args) {

        Date standard = getStandardDate();
        SimpleDateFormat format = new SimpleDateFormat(TIME_STAMP_FORMAT);

        List<String> data = new ArrayList<String>();
        data.add("2015-09-22 08:59:59");
        data.add("2015-09-22 09:00:00");
        data.add("2015-09-22 09:00:01");

        for (String date : data) {
            if(isLate(date, format)) {
                System.out.println(date + " is Late");
            } else {
                System.out.println(date + " is On Time");
            }
        }

    }

    /**
     * check is Late or not
     * */
    public static boolean isLate(String date, SimpleDateFormat format) {
        Date standard = getStandardDate();
        Date inputDate = null;
        boolean result = false;
        try {
            inputDate = format.parse(date);
            if(inputDate.after(standard)) {
                result = true;
            } 
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * get standard date
     * */
    public static Date getStandardDate() {
        Date dateNow = new Date ();
        Calendar cal = Calendar.getInstance();
        cal.setTime(dateNow);
        cal.set(Calendar.DAY_OF_MONTH, 22);
        cal.set(Calendar.HOUR_OF_DAY, 9);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }

Hope this help!

Leave a Reply

Your email address will not be published. Required fields are marked *