判断当前时间是否在某一时间段内

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


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值