zoukankan      html  css  js  c++  java
  • java时间段分成小段存储

    package testThread;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    
    public class testDealTime {
        public static void main(String[] args) throws ParseException {
            List<String> list = new ArrayList<String>();
            list.add("2016-11-25 00:00:00");
            list.add("2016-11-25 00:05:00");
            // list.add("2016-11-25 00:10:00");
            // list.add("2016-11-25 00:15:00");
            list.add("2016-11-25 00:20:00");
            list.add("2016-11-25 00:25:00");
            // list.add("2016-11-25 00:30:00");
            // list.add("2016-11-25 00:35:00");
            list.add("2016-11-25 00:40:00");
            list.add("2016-11-25 00:45:00");
            list.add("2016-11-25 00:50:00");
            list.add("2016-11-25 00:55:00");
            list.add("2016-11-25 01:00:00");
            list.add("2016-11-25 01:05:00");
    
            Calendar cal = Calendar.getInstance();
    
            Map<String, List<String>> map = new HashMap<String, List<String>>();
            List<String> tempList = new ArrayList<String>();
            List<String> tempList2 = null;
            SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            
            for (int i = 0; i < list.size(); i++) {
                if (i == list.size() - 1) {
                    tempList.add(list.get(i));
                    map.put(String.valueOf(i), tempList);
                    break;
                }
                String currTime = list.get(i);
                String nextTime = list.get(i+1);
                Date preDate = simple.parse(currTime);
                cal.setTime(preDate);
                cal.add(Calendar.MINUTE, 5);
                String preTime = String.valueOf(simple.format(cal.getTime()));
                if (nextTime.equals(preTime)) {
                    tempList.add(currTime);
    
                } else {
                    tempList.add(currTime);
                    tempList2 = new ArrayList<String>();
                    tempList2.addAll(tempList);
                    map.put(String.valueOf(i), tempList2);
                    tempList.clear();
                }
            }
            
    
            for (Entry<String, List<String>> entry : map.entrySet()) {
                List<String> lis = entry.getValue();
                for(String createtime : lis){
                    System.out.print(createtime +"  ,");
                }
                System.out.println();
            }
    
        }
    }
  • 相关阅读:
    【JAVASCRIPT】JS实现淘宝,百度评分功能
    【数据结构】链式线性表的几种常用用法
    【JAVASCRIPT】无刷新评论
    【JAVASCRIPT】表单序列化问题
    【JAVASCRIPT】如何不使用jquery函数和ajax框架实现ajax效果
    图灵北京作译者交流会
    是起点,而非终点——评《程序员的思维修炼》
    2011图灵新春特献
    图灵2011.01书讯
    图灵2010.12书讯
  • 原文地址:https://www.cnblogs.com/cxxjohnson/p/6102911.html
Copyright © 2011-2022 走看看