从一段时间段中获取所有日期

写的一个工具,从一个时间段内获取这些详细的日期list

  1     
  2      /**
  3      * 从一段日期中,获取每个日期在整个时间段内的占比
  4      * ---------------------------------------------------------------------
  5      * -- Example:<br>
  6      * -- 输入 20190313 12:00:00 和 20190315 12:00:00 
  7      * -- 输出 [20190313,0.25] [20190314,0.5] [20190315,0.25] 
  8      * --------------------------------------------------------------------- 13      */
 14     public static HashMap<String, Double> getDateAndRate(String stime,String etime) throws ParseException{
 15         HashMap<String, Double> map=new HashMap<String, Double>();
 16         SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd HH:mm:ss");
 17         long s0=df.parse(stime).getTime();
 18         long s1=df.parse(etime).getTime();
 19         long all=s1-s0;
 20         
 21         ArrayList<String> list =getDateListBetween2Day(stime,etime);
 22         if(list.size()==1) {
 23             map.put(list.get(0), 1.00D);
 24         }else {
 25             for(int i=1;i<list.size()-1;i++) {
 26                 double res=86400000.00D/all;
 27                 map.put(list.get(i), res);
 28             }
 29             Double s0_rate=1.00D*getSecondEnd(s0)/all;
 30             map.put(list.get(0), s0_rate);
 31             Double s1_rate=1.00D*getSecondStart(s1)/all;
 32             map.put(list.get(list.size()-1), s1_rate);
 33         }
 34         return map;
 35     }
 36     
 37     /**
 38      * 从一段时间段中获取所有日期
 39      * ---------------------------------------------------------------------
 40      * -- Example:
 41      * -- 输入 20190313 12:00:00 和 20190315 12:00:00 
 42      * -- 输出 20190313 20190314 20190315 
 43      * --------------------------------------------------------------------- 48      */
 49     public static ArrayList<String> getDateListBetween2Day(String stime,String etime) throws ParseException{
 50         return getDateListBetween2Day(stime,etime,"yyyyMMdd");
 51     }
 52     
 53     public static ArrayList<String> getDateListBetween2Day(String stime,String etime,String date_pattern) throws ParseException{
 54         ArrayList<String> list= new ArrayList<String>();
 55         SimpleDateFormat df=new SimpleDateFormat(date_pattern);
 56         Calendar cal = Calendar.getInstance();
 57         Date sdate=df.parse(stime);
 58         Date edate=df.parse(etime);
 59         cal.setTime(sdate);
 60             while(edate.after(sdate)) {
 61                 list.add(df.format(sdate));
 62                 cal.add(Calendar.DAY_OF_MONTH,1);
 63                 sdate=cal.getTime();
 64             }
 65             list.add(df.format(edate));
 66         return list;
 67     }
 68     
 69     /**
 70      * 获取该时间戳距当天结束的时间
 71      * ---------------------------------------------------------------------
 72      * -- Example:<br>
 73      * -- 输入 20190313 12:00:00 的longtime值
 74      * -- 输出 20190313 12:00:00距20190313 24:00:00的毫秒差
 75      * ---------------------------------------------------------------------
 78      */
 79     public static long getSecondEnd(long longdate){
 80           Calendar cal = Calendar.getInstance();
 81           cal.setTimeInMillis(longdate);
 82           cal.set(Calendar.HOUR_OF_DAY, 24);
 83           cal.set(Calendar.MINUTE, 0);
 84           cal.set(Calendar.SECOND, 0);
 85           long max =cal.getTimeInMillis();
 86         return max-longdate;
 87     }
 88     
 89    /**
 90      *  获取该时间戳距当天开始的时间  
 91      * ---------------------------------------------------------------------
 92      * -- Example:
 93      * -- 输入 20190313 12:00:00 的longtime值
 94      * -- 输出 20190313 12:00:00距20190313 00:00:00的毫秒差
 95      * ---------------------------------------------------------------------
 98      */
 99     public static long getSecondStart(long longdate){
100         Calendar cal = Calendar.getInstance();
101           cal.setTimeInMillis(longdate);
102           cal.set(Calendar.HOUR_OF_DAY, 0);
103           cal.set(Calendar.MINUTE, 0);
104           cal.set(Calendar.SECOND, 0);
105           long min =cal.getTimeInMillis();
106       return longdate-min;
107   }

 

posted @ 2019-03-19 17:01  Nucky_yang  阅读(1211)  评论(0编辑  收藏  举报