/**
*
* Title: processingPeriod
* Description: 判断一组时间,按照1分钟分几个时间段
* @param string
* @return
* version V1.0
*/
public static String processingPeriod(String string) {
SimpleDateFormat dfs = new SimpleDateFormat("HH:mm:ss");
String strls = "";
String[] strs = string.split(",");
if(strs.length == 1) {
return strs[0]+" "+strs[0];
}else if(strs.length > 1){
String strhead = strs[0];
String strstart = strs[0];
String strend = "";
int y = 0;
for(int x=1 ; x<strs.length ; x++) {
strend= strs[x];
try {
if(((dfs.parse(strend).getTime() - dfs.parse(strstart).getTime()) /1000)/60 == 1) {
strstart = strend;
y++;
}else {
if(y >0) {
strls +=strhead+"-"+strstart+",";
y = 0;
}else {
strls +=strhead+"-"+strhead+",";
y = 0;
}
if(x+1 <strs.length) {
strhead = strs[x];
strstart = strs[x];
}
}
} catch (ParseException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
if(y != 0) {
strls +=strhead+"-"+strend+",";
}else {
strls +=strend+"-"+strend+",";
}
}
return strls.substring(0, strls.length()-1);
}
public static void main(String[] args) throws Exception {
String str ="8:00:00,09:59:00,10:00:00,10:01:00,14:26:00,14:27:00,14:28:00,14:29:00,14:30:00,14:31:00,14:32:00,14:33:00,14:34:00,14:35:00,14:36:00,14:37:00,14:38:00,14:40:00,14:41:00,14:42:00,14:44:00,14:45:00,14:46:00,14:47:00,14:48:00,14:49:00,14:50:00,14:51:00,14:53:00,14:54:00,14:55:00,14:56:00,14:57:00,14:58:00,14:59:00,15:00:00,15:01:00,15:02:00,15:03:00,15:04:00,15:06:00,15:08:00,15:09:00,15:12:00,15:13:00,15:14:00,15:15:00,15:16:00,15:17:00,15:18:00,15:19:00,15:20:00,15:21:00,15:22:00,15:23:00,15:24:00,15:25:00,15:26:00,15:27:00,15:28:00,15:29:00,15:30:00,15:31:00,15:32:00,15:33:00,15:34:00";
System.out.println(processingPeriod(str));
}
结果:
8:00:00-8:00:00,09:59:00-10:01:00,14:26:00-14:38:00,14:40:00-14:42:00,14:44:00-14:51:00,14:53:00-15:04:00,15:06:00-15:09:00