0

I am working on something where I need the user to input values in to the Terminal Window such as:

Start Time: 0800 End Time: 1200

Then I need to check if this person's start and end time fall within a range. So if the working hours are 0900 - 1700 and it is now 11:45 the above user would be shown as available, whereas if it was 1201 the above user would be unavailable.

Currently I just pull the times in as a string:

void setWorkHours(String hrs)
    {
        this.hours=hrs;
    }

    public String getWorkHours()
    {
        return hours;
    }

Any help much appreciated.

Cheers

user1295053
  • 303
  • 1
  • 7
  • 17
  • It would help the clarity of your question if you were consistent in your examples. 12:01 falls within one of the example ranges 0900-1700, and outside the other (0800-1200) – pamphlet Mar 15 '13 at 15:42
  • So the user works 0800-1200 and the office hours are 0900-1700, if it is 1159 the user would be marked as being available to work but at 1201 they have finished so are unavailable. – user1295053 Mar 15 '13 at 15:45
  • if the user hours are 1400-2300 then they would be marked as being available until 1700 and then after that they would be available for onCall. – user1295053 Mar 15 '13 at 15:46

3 Answers3

2

It would be better if the user could input a start date and time and an end date and time. This would allow a user to enter their entire week at one time.

Nevertheless, the solution is pretty much the same.

  • Convert the start time (date / time) to a Calendar instance.
  • Convert the end time (date / time) to a Calendar instance.
  • Use the Calendar after and before methods to determine if the time (date / time) entered is in between the start time and end time.
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
1

I am not going to do your homework for you, but I'll offer you the following suggestions:

  1. You are going to need to get your input from the terminal (look at System.in)
  2. You are going to need to parse the input. To do this, you need to decide what is valid input. "0800", "08:00", "08.00", "8:00", "8:00 AM", "07:60", "eight o'clock"... which of these are valid, and which will you reject? What is your error handling strategy? Just blow up the application, or provide a nice message to the user?
  3. What datatype are you doing to parse the input into? You might have four ints: startHours, startMins, endHours, endMins. Or maybe you will use Java's Date or Calendar object. There are other options, you need to pick the one that works best for you.
  4. Do you care about seconds? Milliseconds?
  5. Once you have parsed the office hours, you will need some method that given a time determines whether you are inside or outside hours. Presumably this is a separate input. You'll need to parse it (hint, use a utility method for the parsing, and reuse it here), and then pass it to a method that does some calculation. Something like bool isInOfficeHours(int hours, in mins). The parameters to this method should probably match the datatype you're using to store the office hours.

I hope this helps you.

pamphlet
  • 2,054
  • 1
  • 17
  • 27
0

Below is the code that will take two strings in startTime and endTime and will compare to range objects you will have to define your range whatever you like. I have placed comments to explain the code.

     /*
      * this method will split hhmm time into two parts. 
      */
 public String[] getTimeHHMM(String time){   
      String hhmm[] = new String[2];

      if(time !=null && time.length() > 1){
          hhmm[0] = time.substring(0, time.length() - 2);
          hhmm[1] = time.substring(time.length() - 2, time.length());
      }
      else{
          // time not formatted correctly so deal with here
          hhmm[0] = "";
          hhmm[1] = time;
      }
      return hhmm;
 }

     //assuming hrs is a string containing only one time in the format hhmm
     String startTime[] = getTimeHHMM(startTimeStr);
     String endTime[] = getTimeHHMM(endTimeStr);


    int startTimeHrs =  Integer.parseInt(startTime[0]);
     int startTimeMins = Integer.parseInt(startTime[1]);

     int endTimeHrs = Integer.parseInt(endTime[0]);
     int endTimeMins = Integer.parseInt(endTime[1]);

     Date start = new Date();
     Date end = new Date();


 Calendar start = Calendar.getInstance();
 start.set(Calendar.HOUR_OF_DAY, startHrs);
 start.set(Calendar.MINUTE, startMins );

 Calendar end = Calendar.getInstance();
 end.set(Calendar.HOUR_OF_DAY, endHrs);
 end.set(Calendar.MINUTE, endMins );

 ///lets say the range is startRange and endRange it should be Calendar instances, you will need to construct these as I did above with setting your range whatever you like
 Calendar endRange;
 Calendar startRange;

 if(start.compareTo(startRange) >= 0 && end.compareTo(endRange) <=0 ){

    // Here it means it is within working hours


 }
user_CC
  • 4,686
  • 3
  • 20
  • 15