Search code examples
javadatecalendartimestamp

Java code to check if event timestamp falls in weekdays and business hours from 9:30 AM to 4:00 PM


What is the best way in Java to check if event timestamp falls in weekdays and business hours from 9:30 AM to 4:00 PM and name the variable like Event_Type = "business" or "non business" based on the comparison.

1) Time stamps are Unix Time stamps.

2) Time zone differences are not a concern as all event time stamps abelong to the same timezone (Chicago Central TimeZone).

3) Monday, Tuesday, Wednesday, Thursday and Friday are business days. Remaining days are not. Within in these business days if timestamp falls between 9:30 AM to 4:00 PM then event_type varible is equal to "business" other wise it should be non business.

I am looking for the best way to do this efficiently for a large amount of data.

I am looking for something using java.util Calendar Object


Solution

  • Here's an option

        Calendar d = Calendar.getInstance();
        Calendar begin = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
    
        begin.set(Calendar.HOUR, 9);
        begin.set(Calendar.MINUTE, 29);
        begin.set(Calendar.AM_PM, Calendar.AM);
    
        end.set(Calendar.HOUR, 4);
        end.set(Calendar.MINUTE, 01);
        end.set(Calendar.AM_PM, Calendar.PM);
    
        if (d.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY || d.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY
                || (d.after(begin) && d.before(end))) {
            System.out.println("YAY!");
        }