/* 根据一组时间点的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;
}
根据多个时间取距当前最近的
最新推荐文章于 2024-11-06 18:02:02 发布
该代码段提供了一个Java方法,用于从给定的时间点列表中找到距离当前时间最近的已过去的时间点。时间点格式为HHmm,如1100表示11点整。方法首先将当前时间转换为日期对象,然后遍历列表,计算每个时间点与当前时间的差距,保存最近的时间点。

3846

被折叠的 条评论
为什么被折叠?



