下面的代码将在 startTime 和 endTime 中采用两个字符串,并将与范围对象进行比较,您必须根据需要定义范围。我已经发表评论来解释代码。
/*
* 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
}