根据多个时间取距当前最近的
来源:互联网 发布: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
- 根据多个时间取距当前最近的
- Oracle根据当前时间获取最近5年,最近6个月,最近30天的时间
- mysql 取一条离当前时间最近的记录
- 根据当前时间计算年龄的函数
- 取当前时间的java代码
- JS 取当前日期、时间的代码
- JS 取当前日期、时间的代码
- JS 取当前日期、时间的代码
- JS 取当前日期、时间的代码
- JS 取当前日期、时间的代码
- JS 取当前日期、时间的代码
- mysql取系统当前时间的函数
- js 取当前时间的整数
- 取当前时间的前几天、后几天
- mysql取系统当前时间的函数
- 关于系统取当前时间的考虑
- mysql取系统当前时间的函数
- java中获取距离当前时间最近的时间
- 运行此安装程序之前,必须安装 32 位 Windows 映像处理组件(WIC)解决办法
- JavaScript学习 5.2.3 栈方法
- oracle多表插入
- 如何设置dedecms织梦文章页显示“本文链接地址”
- SQL左外链接右链接迫切左外链接
- 根据多个时间取距当前最近的
- android 对话弹出框(Dialog AlertDialog PopupWindow)
- Java里的CompareAndSet(CAS)
- Android系统下的动态Dex加载
- excel统计数据的拷贝和粘贴方法
- MySQL存储过程详解 mysql 存储过程
- matlab size
- 把PDF转成PPT如何转换
- 动态规划解决钢条切割问题