发表于: 2018-01-15 21:52:35

1 612


今天完成的事:

       讨论解决了一些小问题。

  


这里写了一个简单的例子,到时候就按照这个思路来写

public static void main(String[] args) {
   Student s = new Student();
   Student student1 = new Student(1, "Aaron", 240000);
   Student student2 = new Student(2, "Aaron", 220000);
   Student student3 = new Student(3, "Aaron", 230000);
   List<Student> list1=new ArrayList<Student>();
   ss.add(student1);//把这几个学生装进student的list集合
   ss.add(student2);
   ss.add(student3);
   //我们多定义了一个基础类studentt比原来的基础类student多一个字段。
   List<Studentt> list2 = new ArrayList<Studentt>();//申明studentt的list
   for (Student student : ss) {
       Studentt st=new Studentt();
       BeanUtils.copyProperties(student,st);//遍历中用beanutils把list1的东西装进list2

       Long a=st.getAge();

       //模拟拿出时间戳,然后调用我们写的转换的方法,把他变成我们需要的时间数据字段

       String g=TimeFormatUtil.main(a);

       //然后把要加入的新的字段插进到里面

       st.setInv(g);
       list2.add(st);
   }

   System.out.println(list2);//第一次输出

   Map<Object,Object> map=new TreeMap();
   //把list集合转装到treemap中这样,他会自动进行倒序排列。
   for(Studentt t:list2 ){
       map.put(t.getAge(),t);
   }
   System.out.println(map);//第二次输出
   //把map装进json,完成
   JSONObject json = new JSONObject();
   json.put("code",200);
   json.put("data",map);
   System.out.println(json.toString());//第三次输出

三次输出可以看到我们的变化:

第一次:这次是没有排序的,只是加入一条inv的字段。

[Studentt{id=1, name='Aaron', age=240000, inv='4分钟前'}, Studentt{id=2, name='Aaron', age=220000, inv='3分钟前'}, Studentt{id=3, name='Aaron', age=230000, inv='3分钟前'}]

第二次:加入map后已经有了排序,这样给前端的数据就是排好了的。

{220000=Studentt{id=2, name='Aaron', age=220000, inv='3分钟前'}, 230000=Studentt{id=3, name='Aaron', age=230000, inv='3分钟前'}, 240000=Studentt{id=1, name='Aaron', age=240000, inv='4分钟前'}}

第三次:json

{"code":200,"data":{220000:{"age":220000,"id":2,"inv":"3分钟前","name":"Aaron"},230000:{"age":230000,"id":3,"inv":"3分钟前","name":"Aaron"},240000:{"age":240000,"id":1,"inv":"4分钟前","name":"Aaron"}}}


然后转化时间的工具贴一下:

public static String  main(Long l) {
// 定义最终返回的结果字符串。
     String interval = null;
     long s =new Date().getTime()-l;
     long millisecond = new Date().getTime() - s;
     System.out.println(millisecond);
     long second = millisecond / 1000;
     System.out.println(second);

     if (second <= 0) {
     second = 0;
     }

     if (second == 0) {
     interval = "刚刚";
     } else if (second < 30) {
     interval = second + "秒以前";
     } else if (second >= 30 && second < 60) {
     interval = "半分钟前";
     } else if (second >= 60 && second < 60 * 60) {//大于1分钟 小于1小时
        long minute = second / 60;
        interval = minute + "分钟前";
     } else if (second >= 60 * 60 && second < 60 * 60 * 24) {//大于1小时 小于24小时
        long hour = (second / 60) / 60;
        if (hour <= 3) {
     interval = hour + "小时前";
        } else {
     interval = "今天" + getFormatTime(s, "HH:mm");
        }
} else if (second >= 60 * 60 * 24 && second <= 60 * 60 * 24 * 2) {//大于1D 小于2D
        interval = "昨天" + getFormatTime(s, "HH:mm");
     } else if (second >= 60 * 60 * 24 * 2 && second <= 60 * 60 * 24 * 7) {//大于2D小时 小于 7天
        long day = ((second / 60) / 60) / 24;
        interval = day + "天前";
     } else if ( second <= 60 * 60 * 24 * 365 && second >= 60 * 60 * 24 * 7) {//大于7天小于365天
        interval = getFormatTime(s, "MM-dd HH:mm");
     } else if (second >= 60 * 60 * 24 * 365) {//大于365天
        interval = getFormatTime(s, "yyyy-MM-dd HH:mm");
     } else {
     interval = "0";
     }
     System.out.println(interval);
     return interval;
     }
     public static String getFormatTime(Long l, String Sdf) {
     return (new SimpleDateFormat(Sdf)).format(l);
     }

明天计划的事:

        继续看看有什么问题,然后开始写方案评审

问题:

        暂无

收获:

        一些解决问题的思路。


返回列表 返回列表
评论

    分享到