根据多个时间取距当前最近的

该代码段提供了一个Java方法,用于从给定的时间点列表中找到距离当前时间最近的已过去的时间点。时间点格式为HHmm,如1100表示11点整。方法首先将当前时间转换为日期对象,然后遍历列表,计算每个时间点与当前时间的差距,保存最近的时间点。
/* 根据一组时间点的list,取距当前最近的时间(已经结束的最近的时间)
   时间点的格式为HHmm,例如11点整即1100,下午两点半即1430
*/
public String getNearBeforeTime(List<String> timeList)
{
        // 时间点所属的日期
        final String ProfDay = "2014-01-02";  
        Date dateProf = null;		
		String resultTime = "";
        long chaVal = 0;
        
        Date date = new Date();
        String hmsStr = "";
        String hour;
        String minute;
        String second;
        if (date.getHours() < 10) {
            hour = "0" + date.getHours();
        } else {
            hour = "" + date.getHours();
        }
        if (date.getMinutes() < 10) {
            minute = ":0" + date.getMinutes();
        } else {
            minute = ":" + date.getMinutes();
        }
        if (date.getSeconds() < 10) {
            second = ":0" + date.getSeconds();
        } else {
            second = ":" + date.getSeconds();
        }
        hmsStr = hour + minute + second;
        Date nowDate = strToDateLong(profDay + " " + hmsStr);
       
        long tempCha = 0;
        for (int i = 0; i < timeList.size(); i++) {
		    String timeTemp = (String)timeList.get(i);
         
            if(null == timeTemp|| "".equals(timeTemp))
            {
                continue;
            }
            dateProf = strToDateLong(ProfDay + " " + timeStr(timeTemp));
            chaVal = nowDate.getTime() - dateProf.getTime();

            if (i == 0) {
                tempCha = chaVal;
                if (chaVal > 0) {                 
					resultTime = timeTemp;
                }

            } else {
                if (chaVal > 0 && tempCha >0 && chaVal < tempCha) {                    
                    tempCha = chaVal;                 
					resultTime = timeTemp;
                }
                if(chaVal >0 && tempCha <0)
                {
                    tempCha = chaVal;                 
					resultTime = timeTemp;
                }
            }

        }
        return resultTime;
}

/**
     * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
     *
     * @param strDate
     * @return
     */
    public Date strToDateLong(final String strDate)
    {
        java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.text.ParsePosition pos = new java.text.ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

 /**在现有的时间列表中取距hmsStr时间之后最近的时间
     * @param timeList 现有的时间列表
     * @param hmsStr 
     * @return
     */
    public String getNearAfterTime(List <String> timeList,String hmsStr) {
        final String profDay = "2014-01-03";
        String resultTime = "";
        long chaVal = 0;
        Date dateProf = null;
        Date nowDate = strToDateLong(profDay + " " + hmsStr);
        long tempCha = 0;
        for (int i = 0; i < timeList.size(); i++) {          
            String timeTemp =(String)timeList.get(i);
            if(null ==timeTemp ||  "".equals(timeTemp))
            {
                continue;
            }
            dateProf = strToDateLong(profDay + " " + timeStr(timeTemp));
            chaVal = dateProf.getTime() - nowDate.getTime() ;

            if (i == 0) {
                tempCha = chaVal;
                if (chaVal > 0) {
                    cfg = timeCfg;
                }
            } else {
                if (chaVal > 0 && tempCha >0 && chaVal < tempCha) {
                    tempCha = chaVal;
                    resultTime= timeTemp;
                }
                if(chaVal >0 && tempCha <0)
                {
                    tempCha = chaVal;
                    resultTime= timeTemp;
                }
            }

        }
        return cfg;
    }
    /**
     * 时分秒格式转换hhmm->hh:mm:ss
     *    
     */
    public String timeStr(String time) {

        String timeStr = "00:00:00";
        if (StringUtil.isNotEmpty(time) && time.length() <= 4) {
            if (time.length() == 3) {
                time = "0" + time;
            } else if (time.length() == 2) {
                time = "00" + time;
            } else if (time.length() == 1) {
                time = "000" + time;
            }
            timeStr = time.substring(0, 2) + ":" + time.substring(2, 4) + ":00";
        }
        return timeStr;

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值