分割时间工具

根据开始时间和结束时间 分割成若干随机时间

 public static List randomDateBySize(String start,String end,int size,Long id) throws ParseException {
        //输入 随机起始时间
        //解析时间
        Date d1 = simpleDateFormat.parse(start);
        long before = d1.getTime();
        //获取当前时间
        Date d2 = simpleDateFormat.parse(end);
        long after = d2.getTime();

        long randomDate = after-before;
        int[] randomArray = RandomUtils.getRandomArray((int) randomDate, size);
        Listlist=new ArrayList<>();
        for(int index:randomArray){
            PullUserConfigEntity pullUserConfig=new PullUserConfigEntity();

            pullUserConfig.setStartTime(TimeUtils.dateToStrLong(d1));
            d1.setTime(d1.getTime()+index);
            pullUserConfig.setEndTime(TimeUtils.dateToStrLong(d1));
            pullUserConfig.setId(id);
            list.add(pullUserConfig);
        }
        return list;
    }

根据开始时间和结束时间和固定间隔时间 分割成多个时间

 /**
     * 获取固定间隔时刻集合
     * @param start 开始时间
     * @param end 结束时间
     * @param interval 时间间隔(单位:分钟)
     * @return
     */
    public static List< RobotConfigRelEntity> getIntervalTimeList(String start,String end,int interval,Long id){
        Date startDate = convertString2Date("yyyy-MM-dd HH:mm:ss",start);
        Date endDate = convertString2Date("yyyy-MM-dd HH:mm:ss",end);
        List< String> s = new ArrayList<>();
        while(startDate.getTime()<=endDate.getTime()){
            s.add(convertDate2String("yyyy-MM-dd HH:mm:ss",startDate));
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            calendar.add(Calendar.SECOND,interval);
            if(calendar.getTime().getTime()>endDate.getTime()){
                if(!startDate.equals(endDate)){
                    s.add(convertDate2String("yyyy-MM-dd HH:mm:ss",endDate));
                }
                startDate = calendar.getTime();
            }else{
                startDate = calendar.getTime();
            }

        }

        List list = new ArrayList();
        for (int i = 0; i < s.size();i++ ) { //s.length=6,0+1,2+3,4+5
            RobotConfigRelEntity configRel=new RobotConfigRelEntity();
            if(i==0){
                configRel.setStartTime(s.get(i));
                configRel.setEndTime(s.get(i+1));
            }else{
                configRel.setStartTime(s.get(i-1));
                configRel.setEndTime(s.get(i));
                configRel.setRelationId(id);
                list.add(configRel);
            }

        }
        return list;
    }


    public static Date convertString2Date(String format, String dateStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        try {
            Date date = simpleDateFormat.parse(dateStr);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String convertDate2String(String format,Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

判断当前时间是否在开始和结束时间范围

 /**
     * 判断当前时间是否在[startTime, endTime]区间,注意三个参数的时间格式要一致
     * @param nowTime
     * @param startTime
     * @param endTime
     * @return 在时间段内返回true,不在返回false
     */
    public static boolean isEffectiveDate(String startTime, String endTime) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = sdf.format(new Date());
        Date nowTime = sdf.parse(now);
        Date startTime1 = sdf.parse(startTime);
        Date endTime1 = sdf.parse(endTime);

        if (nowTime.getTime() == startTime1.getTime()
                || nowTime.getTime() == endTime1.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime1);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime1);

        return date.after(begin) && date.before(end);
    }

你可能感兴趣的:(windows)