Java验证时间段问题
2009-12-04 16:52:49 来源:WEB开发网核心提示: 在处理监测数据实时录入时,需要提供当前班次信息{大白班,小夜班,大夜班},班次信息是根据给定时间段进行设定类似{{"8:00","16:00"},{"16:00","00:00"},{"00:00","8:0
在处理监测数据实时录入时,需要提供当前班次信息{大白班,小夜班,大夜班},班次信息是根据给定时间段进行设定类似{{"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;
}
}
处理办法
· 取当前时间、转换验证起始、结束时间进行比较。
相关代码
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;
}
}
更多精彩
赞助商链接