欢迎使用CSDN-markdown编辑器

来源:互联网 发布:ubuntu安装xwindow 编辑:程序博客网 时间:2024/06/03 18:53
package abstr;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Collections;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;public class Test {    public static void main(String a[]){        //周期        int t = 3;        //次数        int num1 = 5;        int num2 = 10;        //        int month = 20;        List<User> list = getUsers(t, num1, num2);//包含这个周期内所有用户的活跃度        List<User> newUserList = getNewUser(list,month);//包含新用户的list    }    private static List<User> getNewUser(List<User> list, int month) {        //计算3个月之前是哪天        Date now = new Date();        Calendar cal = Calendar.getInstance();        cal.setTime(now);        cal.add(Calendar.DAY_OF_MONTH, -month);        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置你想要的格式        String t = df.format(now);        List<User> res = new ArrayList<User>();        for (User user : list){            List<User> l = queryUserByTime(user.getName(),t);            if (l.size() == 0){                res.add(user);            }        }        return res;    }    private static List<User> queryUserByTime(String name1111, String t) {        //查询数据库        //"select * from t_user where name='name1111' and login_time < t"        return Collections.emptyList();    }    public static List<User> getUsers(int times,int min,int max){        //今天        Date now = new Date();        Calendar cal = Calendar.getInstance();        cal.setTime(now);        cal.add(Calendar.DAY_OF_MONTH, -times);        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置你想要的格式        String t1 = df.format(cal.getTime());        String t2 = df.format(now);        System.out.println(t1 + "\n" + t2);        //返回值        List<User> list = new ArrayList<User>();        Map<User, Integer> map = queryUsersByCondition(t1,t2);        for(Map.Entry<User, Integer> entry : map.entrySet()){            int sum = entry.getValue();            User user = entry.getKey();            if(sum > max){//活跃用户                user.setType(Type.A);            }else if(sum < min){//僵尸用户                user.setType(Type.C);            }else{//一般用户                user.setType(Type.B);            }            list.add(user);        }        return list;    }    private static Map<User, Integer> queryUsersByCondition(String t1, String t2) {        //模拟用t1和t2查询数据库        //select name,count(*) from t_user where login_time between t1 and t2 group by name;        Map<User,Integer> map = new HashMap<User,Integer>();        map.put(new User("aaa"), 2);        map.put(new User("bbb"), 5);        map.put(new User("ccc"), 1);        map.put(new User("ddd"), 7);        return map;    }}class User{    private String name;    private String type;    public User(String name){        this.name = name;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getType() {        return type;    }    public void setType(String type) {        this.type = type;    }}class Type{    public final static String A = "a";    public final static String B = "b";    public final static String C = "c";}
0 0
原创粉丝点击