0

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
nubteens
  • 5,462
  • 4
  • 20
  • 31
  • Does the date also matter? I mean 2015-09-22 09:00:01 is 23 hours, 59 minutes and 59 seconds early (or on time) for 2015-09-23. Also, [what have you tried](http://mattgemmell.com/what-have-you-tried/)? – Elliott Frisch Sep 24 '15 at 01:28
  • `Date#after/before` or `LocalDateTime#isAfter/isBefore` or `TimeStamp#after/before` ... – MadProgrammer Sep 24 '15 at 01:31
  • The date does not matter. I haven't really tried anything yet but I can think of a solution which is very complicated, which is to get the last 8 index `09:00:01` and to manually check `if(date.substring(0, 2) < 09)`, etc – nubteens Sep 24 '15 at 01:32

4 Answers4

1

You can have code similar to following

// Get the provided date and time
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar providedDate  = Calendar.getInstance();
providedDate.setTime(df.parse(stringInstanceRepresentingDate));

// Get the current date and time
Calendar cal = Calendar.getInstance();

// Set time of calendar to 09:00
cal.set(Calendar.HOUR_OF_DAY, 9);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

// Check if current time is after 09:00 today
boolean afterNine = providedDate.after(cal);

if (afterNine ) {
    System.out.println("You are late");
}
else {
    System.out.println("You are not late");
}
Balwinder Singh
  • 2,272
  • 5
  • 23
  • 34
1

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!

Kenny Tai Huynh
  • 1,464
  • 2
  • 11
  • 23
0

Assuming the input is a String, you can do the following

String input = ...;
String time = input.split(" ")[1],
    hour = time.split(":")[0],
    minute = time.split(":")[1],
    seconds = time.split(":")[2];

if(Integer.parseInt(hour) > 9 && Integer.parseInt(minute) > 0) {
    // Not on time
} 

This is assuming the input is always perfect though, you should use try-catch blocks or check the input beforehand as this could throw exceptions.

Deximus
  • 455
  • 5
  • 8
0
LocalDateTime time = LocalDateTime.of(2015, 9, 22, 9, 0, 0);
LocalDateTime now = LocalDateTime.now();

if (now.compareTo(time) > 0)
    System.out.println("You are late");
else System.out.println("You are in time");

If you are working with strings you have to split them first, as mentioned by @Deximus

Alex R
  • 45
  • 1