在處理監測資料即時輸入時,需要提供當前班次資訊{大白班,小夜班,大夜班},班次資訊是根據給定時間段進行設定類似{{"8:00","16:00"} ,{"16:00","00:00"},{"00:00","8:00"}}
處理辦法
· 取目前時間、轉換驗證起始、結束時間進行比較。
相關程式碼
view plaincopy to clipboardprint?
/**
* 時段測試
* @author WangYanCheng
* @version 2009-11-20
*/
public class CalendarTimeSubsectionTest {
/**
* 測試入口
* @param args arguments lists
*/
public static void main(String[] args) {
CalendarTimeSubsectionTest ctstObj = new CalendarTimeSubsectionTest();
int resultFlag = ctstObj.doGetShift("");
System.out.println(resultFlag);
}
/**
* 驗證某一時間是否在某一時段
* @param currTime 某一時間
* @param timeSlot 某一時段
* @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);
}
/**
* 班次計算
* @param orgCode 所屬單位
* @return result {1:大夜;2:白班;3:小夜;4:夜班;0:特殊處理}
*/
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;
}
//時段0:白班;1:小夜班;2:大夜班*/
private static String[][] timeSubsection = {{"8:00", "16:00"}, {"16:00", "00:00"}, {"00:00", "08:00" }};
/**
* 日期格式化
* @param calenObj 日期實例
* @param formatStr 格式化字串
* @return result 格式完成的字串
*/
public String doParseDate(Calendar calenObj, String formatStr) {
DateFormat df = new SimpleDateFormat(formatStr);
String result = null;
result = df.format(calenObj.getTime());
return result;
}