Java验证时间段问题

王朝学院·作者佚名  2009-12-05
窄屏简体版  字體: |||超大  

在处理监测数据实时录入时,需要提供当前班次信息{大白班,小夜班,大夜班},班次信息是根据给定时间段进行设定类似{{"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;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航