Blame view

src/main/java/com/objecteye/utils/UserTools.java 1.71 KB
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  package com.objecteye.utils;
  
  import com.alibaba.fastjson.JSONArray;
  import com.objecteye.entity.PageResult;
  
  import java.text.SimpleDateFormat;
  import java.util.ArrayList;
  import java.util.Date;
  import java.util.List;
  
  public class UserTools {
  
      public static JSONArray getJSONArrayByList(List list) {
          JSONArray jsonArray = new JSONArray();
          if (list == null || list.isEmpty()) {
              return jsonArray;
          }
          jsonArray.addAll(list);
          return jsonArray;
      }
  
      public static String nowToDate() {
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          return sdf.format(new Date());
      }
  
      /**
       * 统一手动分页
       *
       * @param baseList    原始数据集合
       * @param currentpage 页码
       * @param pagevolume  页面容量
       * @return 结果集
       */
      public static PageResult makePageResultByBaseList(List<?> baseList, Integer currentpage, Integer pagevolume) {
          // 总页数
          int pageTotal = (baseList.size() / pagevolume) + ((baseList.size() % pagevolume > 0) ? 1 : 0);
  
          int fromIndex = (currentpage - 1) * pagevolume;
          int toIndex;
          if (currentpage == pageTotal) {
              toIndex = baseList.size();
          } else if (pageTotal == 0) {
              return new PageResult<>(pageTotal, new ArrayList<>());
          } else {
              toIndex = currentpage * pagevolume;
              if (toIndex > baseList.size()) {
                  toIndex = baseList.size();
              }
              if (fromIndex >= toIndex) {
                  return new PageResult<>(pageTotal, new ArrayList<>());
              }
          }
  
          return new PageResult<>(pageTotal, baseList.subList(fromIndex, toIndex));
      }
  }