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

来源:互联网 发布:2016年网络购物规模 编辑:程序博客网 时间:2024/06/09 00:13
/* 根据一组时间点的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;    }

0 0
原创粉丝点击