码迷,mamicode.com
首页 > 其他好文 > 详细

时间段去重复合并

时间:2019-03-20 22:21:35      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:size   private   calendar   cal   return   bsp   form   equal   ring   

/**
     * 合并时间段去掉重复的
     *
     * 时间合并 12:00:00----13:00:00   &  12:30:00 --- 14:00:00  =====》 12:00:00----14:00:00
     * @param usertime
     */
    public static void unionTime(List<UserTime> usertime){
        //顺序timesixth在最后,不可以改其他的可以改
        /*List<UserTime> usertime = new ArrayList<UserTime>();
        SimpleDateFormat sdf=new SimpleDateFormat("HH:mm");
        UserTime ut = new UserTime(1, sdf.parse("12:00"), sdf.parse("13:00"));
        UserTime ut2 = new UserTime(1, sdf.parse("12:30"), sdf.parse("15:00"));
        UserTime ut3 = new UserTime(1, sdf.parse("16:00"), sdf.parse("19:00"));
        usertime.add(ut);
        usertime.add(ut2);
        usertime.add(ut3);*/

        timeFirst(usertime);
        timeSecond(usertime);
        timeThird(usertime);
        timeFourth(usertime);
        timefifth(usertime);
        timesixth(usertime);
    }

    private static void timeFirst(List<UserTime> usertime) {
        for(int i=0;i<usertime.size()-1;i++){
            int j=i+1;
            if(usertime.get(i).getUserid()==usertime.get(j).getUserid())
            {
                if(usertime.get(i).getStart().after(usertime.get(j).getStart())
                        && usertime.get(i).getEnd().before(usertime.get(j).getEnd())
                ){
                    //            |------|
                    //          |---------|
                    usertime.remove(i);
                    timeFirst(usertime);
                }
                 /*  if(usertime.get(i).getEnd().before(usertime.get(i).getStart())){
                    // |--------|
                    //            |-------|
                    //不可以动
                }*/

            }
        }
    }

    private static void timesixth(List<UserTime> usertime) {
        for(int i=0;i<usertime.size()-1;i++){
            int j=i+1;
            if(usertime.get(i).getUserid()==usertime.get(j).getUserid()){
                if(usertime.get(i).getEnd().after(usertime.get(j).getStart())
                        && usertime.get(i).getStart().before(usertime.get(j).getStart())
                        &&usertime.get(i).getEnd().before(usertime.get(j).getEnd())
                        &&usertime.get(i).getEnd().before(usertime.get(j).getEnd())){
                    // |-----------|
                    //          |---------|
                    usertime.get(j).setStart(usertime.get(i).getStart());
                    usertime.remove(i);
                    timesixth(usertime);
                }else if(usertime.get(i).getEnd().equals(usertime.get(j).getStart())) {
                    // |------|
                    //        |-----|
                    usertime.get(j).setStart(usertime.get(i).getStart());
                    usertime.remove(i);
                    timesixth(usertime);
                }
                 /*  if(usertime.get(i).getEnd().before(usertime.get(i).getStart())){
                    // |--------|
                    //            |-------|
                    //这种情况不用考虑,就是两个单独的时间
                }*/

            }
        }
    }

    /**
     * // |------|
     * //        |-----|
     */
    private static void time7(List<UserTime> usertime) {
        for(int i=0;i<usertime.size()-1;i++) {
            int j = i + 1;
            if (usertime.get(i).getUserid() == usertime.get(j).getUserid()) {
                if(usertime.get(i).getEnd().equals(usertime.get(j).getStart())) {
                    // |------|
                    //        |-----|
                    usertime.get(j).setStart(usertime.get(i).getStart());
                    usertime.remove(i);
                    time7(usertime);
                }
            }
        }
    }


    private static void timeSecond(List<UserTime> usertime) {
        for(int i=0;i<usertime.size()-1;i++) {
            int j = i + 1;
            if (usertime.get(i).getUserid() == usertime.get(j).getUserid()) {
                if(usertime.get(i).getStart().equals(usertime.get(j).getStart())
                        &&usertime.get(i).getEnd().before(usertime.get(j).getEnd())){
                    // |------------|
                    // |--------------|
                    usertime.remove(i);
                    timeSecond(usertime);
                }
            }
        }
    }

    private static void timeThird(List<UserTime> usertime) {
        for(int i=0;i<usertime.size()-1;i++) {
            int j = i + 1;
            if (usertime.get(i).getUserid() == usertime.get(j).getUserid()) {
                if(usertime.get(i).getStart().equals(usertime.get(j).getStart())
                        &&usertime.get(i).getEnd().after(usertime.get(j).getEnd())){
                    // |------------|
                    // |----------|
                    usertime.remove(j);
                    timeThird(usertime);
                }
            }
        }
    }

    private static void timeFourth(List<UserTime> usertime) {
        for(int i=0;i<usertime.size()-1;i++) {
            int j = i + 1;
            if (usertime.get(i).getUserid() == usertime.get(j).getUserid()) {

                //开始时间排序了,下边这个不考虑,否则考虑
                if(usertime.get(i).getStart().equals(usertime.get(j).getStart())&&usertime.get(i).getEnd().equals(usertime.get(j).getEnd())){
                    // |----------|
                    // |----------|
                    usertime.remove(i);
                    timeFourth(usertime);
                }
            }
        }
    }

    private static void timefifth(List<UserTime> usertime) {
        for(int i=0;i<usertime.size()-1;i++) {
            int j = i + 1;
            if (usertime.get(i).getUserid() == usertime.get(j).getUserid()) {
                //开始时间排序了,下边这个不考虑,否则考虑
                if(usertime.get(i).getStart().before(usertime.get(j).getStart())
                        &&usertime.get(i).getEnd().after(usertime.get(j).getStart())
                        && usertime.get(i).getStart().before(usertime.get(j).getEnd())
                        &&usertime.get(i).getEnd().after(usertime.get(j).getEnd())){
                    //     |----------|
                    //      |-------|
                    usertime.remove(j);

                    timefifth(usertime);
                }else{
                    continue;
                }
            }
        }
    }

    public static class UserTime implements Comparable<UserTime>{
        public int userid;//用户ID
        public String title;//这个是在full calendar插件使用的时候用到,可以忽略
        public Date start;//开始时间
        public Date end;//结束时间
        public UserTime(int userid, Date begintime, Date endtime,String title){
            this.userid=userid;
            this.start=begintime;
            this.end=endtime;
            this.title=title;
        }

        @Override
        public int compareTo(UserTime o) {
            //先用用户ID排序,最后用时间
            if(this.userid!=o.userid)
                return this.userid-o.userid;
            else{
                if(this.start.before(o.end))
                    return -1;
                else
                    return 1;
            }
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public int getUserid() {
            return userid;
        }

        public void setUserid(int userid) {
            this.userid = userid;
        }

        public Date getStart() {
            return start;
        }

        public void setStart(Date begintime) {
            this.start = begintime;
        }

        public Date getEnd() {
            return end;
        }

        public void setEnd(Date endtime) {
            this.end = endtime;
        }

    }

 

时间段去重复合并

标签:size   private   calendar   cal   return   bsp   form   equal   ring   

原文地址:https://www.cnblogs.com/renjianjun/p/10568046.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!