When processing real-time input of monitoring data, you need to provide the current shift information {big day shift, small night shift, big night shift}. The shift information is set based on a given time period, similar to {{"8:00", "16:00"} ,{"16:00","00:00"},{"00:00","8:00"}}
Solution
· Compare the current time, the start and end time of conversion verification.
Related code
view plaincopy to clipboardprint?
/**
* Time period test
* @author WangYanCheng
* @version 2009-11-20
*/
public class CalendarTimeSubsectionTest {
/**
* Test entrance
* @param args arguments lists
*/
public static void main(String[] args) {
CalendarTimeSubsectionTest ctstObj = new CalendarTimeSubsectionTest();
int resultFlag = ctstObj.doGetShift("");
System.out.println(resultFlag);
}
/**
* Verify whether a certain time is in a certain time period
* @param currTime a certain time
* @param timeSlot a certain time period
* @return true/false
*/
public boolean isShift(final long currTime, String[] timeSlot) {
Calendar tempCalendar = Calendar.getInstance();
tempCalendar.setTimeInMillis(currTime);
String[] tmpArray = timeSlot[0].split(":");
long startTime, stopTime;
tempCalendar.clear(Calendar.HOUR_OF_DAY);
tempCalendar.clear(Calendar.MINUTE);
tempCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(tmpArray[0]));
tempCalendar.set(Calendar.MINUTE, Integer.parseInt(tmpArray[1]));
startTime = tempCalendar.getTimeInMillis();
tmpArray = timeSlot[1].split(":");
int stopHour = Integer.parseInt(tmpArray[0]), stopMinute = Integer.parseInt(tmpArray[1]);
if (stopHour == 0) {
tempCalendar.add(Calendar.DAY_OF_MONTH, 1);
}
tempCalendar.clear(Calendar.HOUR_OF_DAY);
tempCalendar.clear(Calendar.MINUTE);
tempCalendar.set(Calendar.HOUR_OF_DAY, stopHour);
tempCalendar.set(Calendar.MINUTE, stopMinute);
stopTime = tempCalendar.getTimeInMillis();
return ((startTime < currTime && currTime <= stopTime) ? true : false);
}
/**
*Shift calculation
* @param orgCode affiliation
* @return result {1:big night;2:day shift;3:small night;4:night shift;0:special treatment}
*/
public int doGetShift(String orgCode) {
int result = 0;
Calendar currCalen = Calendar.getInstance();
long currTime = currCalen.getTimeInMillis();
if (isShift(currTime, timeSubsection[2])) {
result = 1;
} else if (isShift(currTime, timeSubsection[0])) {
result = 2;
} else if (isShift(currTime, timeSubsection[1])) {
result = 3;
}
return result;
}
//Time period 0: day shift; 1: small night shift; 2: big night shift*/
private static String[][] timeSubsection = {{"8:00", "16:00"}, {"16:00", "00:00"}, {"00:00", "08:00" }};
/**
* Date formatting
* @param calenObj date instance
* @param formatStr format string
* @return result format completed string
*/
public String doParseDate(Calendar calenObj, String formatStr) {
DateFormat df = new SimpleDateFormat(formatStr);
String result = null;
result = df.format(calenObj.getTime());
return result;
}