个人从近来的工作中整理部分可能对大家有用的算法和使用工具类方法,将持续更新,有好的算法大家一起讨论,哪里不正确,欢迎大家随时指正,我这里也会实时更新一些开发中比较实用的工具类方法给大家,共勉~~

 

  1 package com.ideal.customutils;
  2 
  3 import java.text.ParseException;
  4 import java.text.SimpleDateFormat;
  5 import java.util.Calendar;
  6 import java.util.Date;
  7 import java.util.List;
  8 import java.util.Map;
  9 
 10 import org.apache.commons.lang3.StringUtils;
 11 
 12 public class CalculationUtil
 13 {
 14 
 15     private CalculationUtil()
 16     {
 17         throw new IllegalStateException("CalculationUtil class");
 18     }
 19 
 20     /**   
 21      * @Title: getSort   
 22      * @Description: 将List进行排序[时间类型"yyyy-MM-dd HH:mm:ss"转换成毫秒后的比较排序(数值类型可直接使用)]   
 23      * @param list
 24      * @param sortColunm(需要进行排序的字段)[只有数值型数据可以排序,如不是,请转成毫秒时间戳类型]
 25      * @param desc(true-从大到小/false-从小到大) 
 26      * @return      
 27      * @author: yue_sun 
 28      * @date:   2018年4月18日 下午8:06:44   
 29      */
 30     @SuppressWarnings({ "rawtypes" })
 31     public static List getSortForDate ( List list, String sortColunm, boolean desc )
 32     {
 33         // 判断list不为空且集合的大小大于1时才进行排序操作
 34         if (!list.isEmpty() && list.size() > 1)
 35         {
 36             if (desc)
 37             {
 38                 // 将list数组中指定参数进行倒序排列
 39                 getSortForDateDesc(list, sortColunm);
 40             } else
 41             {
 42                 // 将list数组中指定参数进行正序排列
 43                 getSortForDateAsc(list, sortColunm);
 44             }
 45         }
 46         return list;
 47     }
 48 
 49     /**   
 50      * @Title: getSortForDateDesc   
 51      * @Description: 将list数组中指定参数进行倒序排列
 52      * @param list【要进行排序的list集合】
 53      * @param sortColunm【要进行排序的集合中的参数列】      
 54      * @author: yue_sun 
 55      * @date:   2019年3月26日 上午9:15:29   
 56      */
 57     @SuppressWarnings({ "rawtypes", "unchecked" })
 58     public static void getSortForDateDesc ( List list, String sortColunm )
 59     {
 60         for (int i = 0, m = list.size(); i < m; i++)
 61         {
 62             for (int j = 0, n = list.size() - i - 1; j < n; j++)
 63             {
 64                 Map map1 = (Map) list.get(j);
 65                 Map map2 = (Map) list.get(j + 1);
 66                 String timeStr1 = map1.get(sortColunm).toString();
 67                 String timeStr2 = map2.get(sortColunm).toString();
 68                 long time1 = getTimeInMillis(timeStr1);
 69                 long time2 = getTimeInMillis(timeStr2);
 70                 if (time1 < time2)
 71                 {
 72                     list.set(j, map2);
 73                     list.set(j + 1, map1);
 74                 }
 75             }
 76         }
 77     }
 78 
 79     /**   
 80      * @Title: getSortForDateAsc   
 81      * @Description: 将list数组中指定参数进行正序排列  
 82      * @param list【要进行排序的list集合】
 83      * @param sortColunm【要进行排序的集合中的参数列】         
 84      * @author: yue_sun 
 85      * @date:   2019年3月26日 上午9:15:59   
 86      */
 87     @SuppressWarnings({ "rawtypes", "unchecked" })
 88     public static void getSortForDateAsc ( List list, String sortColunm )
 89     {
 90         for (int i = 0; i < list.size(); i++)
 91         {
 92             for (int j = 0; j < list.size() - 1; j++)
 93             {
 94                 Map map1 = (Map) list.get(i);
 95                 Map map2 = (Map) list.get(j);
 96                 String timeStr1 = map1.get(sortColunm).toString();
 97                 String timeStr2 = map2.get(sortColunm).toString();
 98                 long time1 = getTimeInMillis(timeStr1);
 99                 long time2 = getTimeInMillis(timeStr2);
100                 if (time1 < time2)
101                 {
102                     list.set(i, map2);
103                     list.set(j, map1);
104                 }
105             }
106         }
107     }
108 
109     /**   
110      * @Title: removeDuplicate   
111      * @Description: 清理list中重复数据的方法
112      * @param list
113      * @return      
114      * @author: yue_sun 
115      * @date:   2018年6月26日 下午4:34:37   
116      */
117     @SuppressWarnings("rawtypes")
118     public static List removeDuplicate ( List list )
119     {
120         for (int i = 0; i < list.size() - 1; i++)
121         {
122             for (int j = list.size() - 1; j > i; j--)
123             {
124                 if (list.get(j).equals(list.get(i)))
125                 {
126                     list.remove(j);
127                 }
128             }
129         }
130         return list;
131     }
132 
133     /**   
134      * @Title: getTimeInMillis   
135      * @Description: 将时间类型转成毫秒时间戳类型 
136      * @param dateString
137      * @return      
138      * @author: yue_sun
139      * @date:   2018年4月18日 下午8:04:18  
140      */
141     public static long getTimeInMillis ( String dateString )
142     {
143         Calendar calendar = parseDate(dateString);
144         return calendar == null ? 0 : calendar.getTimeInMillis();
145     }
146 
147     /**   
148      * @Title: parseDate   
149      * @Description: 时间格式转换,将string时间类型转换成毫秒时间戳  
150      * @param dateString
151      * @return      
152      * @author: yue_sun
153      * @date:   2018年4月18日 下午8:00:32  
154      */
155     public static Calendar parseDate ( String dateString )
156     {
157         Calendar calendar = Calendar.getInstance();
158         if (isBlank(dateString))
159         {
160             return null;
161         }
162         try
163         {
164             SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
165             Date date = format.parse(dateString);
166             calendar.setTime(date);
167         } catch (ParseException e)
168         {
169             return null;
170         }
171         return calendar;
172     }
173 
174     /**   
175      * @Title: joinForQuotationMarks   
176      * @Description: 将list生成sql查询条件中的in格式的参数,带引号参数
177      * @param list
178      * @return      
179      * @author: yue_sun
180      * @date:   2019年3月26日 上午8:22:55   
181      */
182     @SuppressWarnings("rawtypes")
183     public static String joinForQuotationMarks ( List list )
184     {
185         StringBuilder strBuilder = new StringBuilder();
186         String result = StringUtils.join(list, ",");
187         // 当生成的参数不为空的时候, 做参数处理操作
188         if (!isBlank(result))
189         {
190             strBuilder.append("'");
191             strBuilder.append(StringUtils.replace(result, ",", "','"));
192             strBuilder.append("'");
193         }
194         return result;
195     }
196 
197     /**   
198      * @Title: isBlank   
199      * @Description: 校验str值是否为null或者空,包含字符串的null
200      * @param str
201      * @return      
202      * @author: yue_sun 
203      * @date:   2019年3月11日 下午6:52:38   
204      */
205     public static boolean isBlank ( String str )
206     {
207         return null == str || StringUtils.equals(str, "null") || StringUtils.isBlank(str);
208     }
209 }

 

posted on 2019-03-26 09:31  书未来  阅读(334)  评论(0编辑  收藏  举报