package cn.com.lzt.common.util; import groovy.time.BaseDuration.From; import java.io.IOException; import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import org.apache.log4j.Logger; /** * 公共静态常量类 * * @author zhaojiayan * @date 2017年8月12日下午12:00:46 * */ public class DataUtil { /** * 将数字保留两位小数 * @param data * @return */ public static String getFormatDecimailTwo(BigDecimal data) { if(data == null){ return null; }else{ DecimalFormat df = new DecimalFormat("#0.00"); return df.format(data); } } /** * 获取某月的自然天数 * @return */ public static Integer getMonthDays(String yearmonth) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); Calendar begincal = Calendar.getInstance(); try { begincal.setTime(format.parse(yearmonth)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return begincal.getActualMaximum(Calendar.DAY_OF_MONTH); } /** * 获取当前月的上一个月yyyy-MM * @return */ public static String getBeforeMonth() { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); Calendar begincal = Calendar.getInstance(); begincal.setTime(new Date()); begincal.add(Calendar.MONTH, -1); String datestr = format.format(begincal.getTime()); return datestr; } /** * 返回月初月未及上个月份信息 * @param month * @return */ public static Map getMonthInfo(String month) { Map params = new HashMap(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); Calendar begincal = Calendar.getInstance(); try { begincal.setTime(format.parse(month)); begincal.set(Calendar.DAY_OF_MONTH, begincal.getActualMinimum(Calendar.DAY_OF_MONTH)); params.put("earlymonth", format1.format(begincal.getTime())); begincal.set(Calendar.DAY_OF_MONTH, begincal.getActualMaximum(Calendar.DAY_OF_MONTH)); params.put("endmonth", format1.format(begincal.getTime())); begincal.add(Calendar.MONTH, -1); params.put("beforemotn", format.format(begincal.getTime())); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return params; } /** * 获取某个季度的年月 * @param year * @param quarters * @return */ public static List getYearMonthList(String year,int quarters) { List list = new ArrayList(); if(quarters == 1){ list.add(year +"-01"); list.add(year +"-02"); list.add(year +"-03"); }else if(quarters == 2){ list.add(year +"-04"); list.add(year +"-05"); list.add(year +"-06"); }else if(quarters == 3){ list.add(year +"-07"); list.add(year +"-08"); list.add(year +"-09"); }else if(quarters == 4){ list.add(year +"-10"); list.add(year +"-11"); list.add(year +"-12"); } return list; } /** * 获取月份集合 * @Title: getMonList * @Description: TODO * @param @param begindate * @param @param enddate * @param @return * @return ArrayList * @throws */ public static ArrayList getMonList(){ ArrayList dates = new ArrayList(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); Date date = new Date(); String begindate= date.getYear()+"-01-01"; String enddate = date.getYear()+"-12-01"; try { Calendar begincal = Calendar.getInstance(); begincal.setTime(format.parse(begindate)); Calendar endcal = Calendar.getInstance(); endcal.setTime(format.parse(enddate)); while(begincal.getTimeInMillis() * @param colls * @return */ public static List getList(Collection colls){ if(colls==null || colls.isEmpty())return Collections.emptyList(); List list = new ArrayList(colls.size()); for(T id:colls){ list.add(id); } return list; } /** * 获得对象列表 * @param * @param ids * @return */ public static List getList(T... ids) { if(ids==null || ids.length<1)return Collections.emptyList(); List list = new ArrayList(ids.length); for (T id : ids) { list.add(id); } return list; } /** * 在原有基础上增加对象 * @param * @param list * @param ids * @return */ public static List getList(List list,T...ids){ if(ids==null || ids.length<1)return list; List resultList = new ArrayList(); resultList.addAll(list); for (T id : ids) { resultList.add(id); } return resultList; } /** * 获得Integer对象列表 * @param ints * @return */ public static List getList(int... ints){ List list = new ArrayList(); for(int i:ints){ list.add(i); } return list; } /** * 获得调用的方法名 * @return */ public static String getInvokeMethodName(){ String methodName = null; try { Thread thr = Thread.currentThread(); StackTraceElement[] ele = thr.getStackTrace(); methodName = ele[ele.length>3?3:2].getMethodName(); } catch (Exception e) { e.printStackTrace(); } return methodName; } /** * 获得调用的类 * @return */ public static Class getInvokeClass(){ Class clazz = null; try { Thread thr = Thread.currentThread(); StackTraceElement[] ele = thr.getStackTrace(); clazz = Class.forName(ele[ele.length>3?3:2].getClassName()); } catch (Exception e) { e.printStackTrace(); } return clazz; } /** * 打印调用的类链 */ public static void printStackInvokeClass(){ try { Thread thr = Thread.currentThread(); StackTraceElement[] ele = thr.getStackTrace(); int index = 0 ; for(StackTraceElement ste:ele){ if(index++>2){ System.out.println(ste); } } } catch (Exception e) { e.printStackTrace(); } } /** * 获得调用的类名 * @return */ public static String getInvokeClassName(){ String clazzName = null; try { Thread thr = Thread.currentThread(); StackTraceElement[] ele = thr.getStackTrace(); clazzName = ele[ele.length>3?3:2].getClassName(); } catch (Exception e) { e.printStackTrace(); } return clazzName; } /** * 获得反转的list * @param * @param list * @return */ public static List getReversionList(List list) { Stack stack = new Stack(); for(T t : list){ stack.push(t); } return getListFromStack(stack); } /** * 获得List :stack转list * @param * @param stack * @return */ public static List getListFromStack(Stack stack){ List l = new ArrayList(); while(!stack.empty())l.add(stack.pop()); return l; } /** * 获得int数组 * @param strings * @return */ public static int[] getIntArray(String...strings){ if(strings==null || strings.length<1)return new int[]{}; int[] array = new int[strings.length]; for(int i=0;i getMap(String...strings) { if(strings==null || strings.length%2!=0)return Collections.emptyMap(); Map map = new LinkedHashMap(); for(int i=0;i it){ if(it==null)return new Integer[0]; List list = new ArrayList(); for(Object obj : it){ list.add(StringUtil.getInt(obj)); } return list.toArray(new Integer[list.size()]); } /** * 获得Integer数组 * @param strings * @return */ public static Integer[] getIntegerArray(String...strings){ if(strings==null)return new Integer[0]; List list = new ArrayList(); for(String str :strings) list.add(StringUtil.getInt(str)); return list.toArray(new Integer[list.size()]); } /** * 获得列表中的某个索引,如果不存在返回null * @param * @param list * @param index * @return */ public static T get(List list , int index){ if(list==null || list.size() * @param * @param map * @param comparator 值排序器 * @return */ public static > Map getSortedByValueMap(final Map map, Comparator comparator) { if(map==null || map.isEmpty() || comparator==null) return map; Map resultMap = new LinkedHashMap(); final List values = new ArrayList(); values.addAll(map.values()); Collections.sort( values, comparator); List keys = new ArrayList(); keys.addAll(map.keySet()); Collections.sort(keys,new Comparator(){ public int compare(K o1, K o2) { int index0 = values.indexOf(map.get(o1)); int index1 = values.indexOf(map.get(o2)); if(index0==index1)return 0; if(index0>index1)return 1; return -1; } }); for(K k:keys){ resultMap.put(k, map.get(k)); } return resultMap; } /** * 获得非空选择对象 * @param * @param t * @param def * @return */ public static T getNonNull(T t,T def) { return t==null?def:t; } /** * 获得循环索引值 * @param current 当前索引值 * @param step 每步值 * @param start 起始值 * @param length 列表长度 * @param zero 是否包含零 * @return -1 为停止 */ public static int getCycleIndex(int current,int step,int start, int length, boolean zero){ if(current == start-1) return -1; int index = current + step; boolean cycle = false; if(index>length || (zero && index >= length)){ index = index - length; cycle = true; } if(cycle && index >= start) return -1; return index; } /** * 获得下一个循环索引,到边界重新开始 * @param current * @param step * @param length * @param zero * @return */ public static int getNextCycleIndex(int current, int step, int length ,boolean zero){ if(length<0 || current>length) return -1; int index = current + step; if(index>length || (zero && index >= length)){ index = index - length; } return index; } /** * 获得list0中有而list1中没有的对象列表 * @param * @param list0 * @param list1 * @return */ public static List getExtList(List list0, List list1) { if(list1==null || list1.isEmpty())return list0; List tList = new ArrayList(); if(list0!=null && !list0.isEmpty()){ for(T t:list0){ if(!list1.contains(t)){ tList.add(t); } } } return tList; } /** * 动态调用方法 * @param target * @param method * @param clazz * @param args * @return */ public static Object getInvokeReturn(Object target,String method,Object...args){ if(target==null) return null; try { args = ValidateUtil.isNullArray(args)?new Object[]{}:args; Class[] clazz = new Class[args.length]; for(int i=0,j=args.length;i targetClazz,String method,Object... args) { try { args = ValidateUtil.isNullArray(args)?new Object[]{}:args; Class[] clazz = new Class[args.length]; for(int i=0,j=args.length;i * @param clazz * @param value * @param t * @return */ @SuppressWarnings("unchecked") public static T getEnumType(Class clazz,String value, T t) { try{ return (T) DataUtil.getInvokeStaticReturn(clazz, "valueOf", value); }catch(Exception e){ return t; } } public static void main(String[] args) throws ParseException { System.out.println(); } }