import java.io.*;
import java.util.*;
public class WorkHourChecker {
private final static long MILLSECS_PER_HOUR = 1L * 60L * 60L * 1000L;
public static void main (String[] args) throws IOException, InterruptedException {
String workHours = "2-5,19-23,14-15";
checkTimeAndWork (workHours);
System.exit(0);
}
private static void checkTimeAndWork (String workHours) throws InterruptedException,
NumberFormatException {
Date now = new Date();
boolean isWorkhour = false;
StringTokenizer hoursTokenizer = new StringTokenizer(workHours,",");
while (hoursTokenizer.hasMoreTokens()) {
String duration = (String) hoursTokenizer.nextElement();
StringTokenizer durationTokenizer = new StringTokenizer(duration,"-");
int startHour = -1;
int endHour = -1;
while (durationTokenizer.hasMoreElements()) {
startHour = Integer.parseInt((String)durationTokenizer.nextElement()) ;
endHour = Integer.parseInt((String)durationTokenizer.nextElement()) ;
}
if (now.getHours() >= startHour && now.getHours() <= endHour){
isWorkhour = true;
break;
}else{
isWorkhour = false;
}
}
if (isWorkhour){
System.out.println ("-- In the work hours ..");
doSomeBusiness ();
System.out.println ("-- I am very tired i will get some rest!!");
Thread.currentThread().sleep (MILLSECS_PER_HOUR);
checkTimeAndWork (workHours);
}else{
System.out.println(" -- Not in the work hours yet, sleep 1 hour");
Thread.currentThread().sleep (MILLSECS_PER_HOUR);
checkTimeAndWork (workHours);
}
}
private static void doSomeBusiness (){
System.out.println (" -- I have started to work..");
}
}
No comments:
Post a Comment