对时间戳分类,并将规定时间段内的时间戳个数统计出来写入txt文件中

 @Test
    public void compare()  {
        String oldPath = "C:/Users/lenovo/Desktop/136.txt";
        String newPath = "C:/Users/lenovo/Desktop/111.txt";
       try {
           BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(newPath));
           BufferedReader bufferedReader = new BufferedReader(new FileReader(oldPath));
           String line;
           //将起始时间以及该时间段内的时间戳个数放入map中
           Map<Long, Integer> map = new HashMap<>();
           long start = 1552968199;
           long code = 60000;
           long changeLine = 0;
           int sum = 0;
           while ((line = bufferedReader.readLine())!= null){
               changeLine = Long.valueOf(line);
               while (changeLine!=0){
                   if((changeLine>=start)&&(changeLine<(start+code))) {
                       sum = ++sum;
                       map.put(start, sum);
                       changeLine = 0;
                   }else {
                       start = start + code;
                       sum = 0;
                   }
               }
           }
           for (Long time : map.keySet()){
               String time11 = formatEpochSecond("yyyy-MM-dd_HH:mm:ss",time);
               bufferedWriter.write(time11+"\t"+ map.get(time) +"\r\n");
           }
               bufferedWriter.flush();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }
//时间戳转换为时间
 public static String formatEpochSecond(String pattern, long epochSecond) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(epochSecond),
                ZoneId.systemDefault());
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

猜你喜欢

转载自blog.csdn.net/qq_39082699/article/details/88965865