java判断一组时间,按照1分钟分组,分几个时间段

      /**
      * 
     * 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

非著名架构师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值