| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588 |
- 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<String,Object> getMonthInfo(String month) {
- Map<String,Object> params = new HashMap<String, Object>();
- 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<String> getYearMonthList(String year,int quarters) {
- List<String> list = new ArrayList<String>();
- 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<String>
- * @throws
- */
- public static ArrayList<String> getMonList(){
- ArrayList<String> dates = new ArrayList<String>();
- 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()<endcal.getTimeInMillis()){
- String d = format.format(begincal.getTime()).substring(5, 7);
- dates.add(d);
- begincal.add(Calendar.MONTH, 1);
- }
- dates.add(enddate.substring(4,6));
- } catch (Exception e) {
- e.printStackTrace();
- }
- return dates;
- }
-
- /**
- * 查询日期是否相差七天
- * @param date
- * @return
- */
- public static Boolean get7DateResult(Date date) {
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- Calendar begincal = Calendar.getInstance();
- begincal.add(Calendar.DAY_OF_MONTH, 7);
- Calendar endcal = Calendar.getInstance();
- endcal.setTime(date);
- String begintime = format.format(begincal.getTime());
- String endtime = format.format(endcal.getTime());
- return begintime.equals(endtime);
- }
-
- /**
- * 获取当前时间
- * @return
- */
- public static String getCurDateString(Date date) {
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- return format.format(date);
- }
- /**
- * 获得对象列表
- * @param <T>
- * @param colls
- * @return
- */
- public static <T> List<T> getList(Collection<T> colls){
- if(colls==null || colls.isEmpty())return Collections.emptyList();
- List<T> list = new ArrayList<T>(colls.size());
- for(T id:colls){
- list.add(id);
- }
- return list;
- }
- /**
- * 获得对象列表
- * @param <T>
- * @param ids
- * @return
- */
- public static <T> List<T> getList(T... ids) {
- if(ids==null || ids.length<1)return Collections.emptyList();
- List<T> list = new ArrayList<T>(ids.length);
- for (T id : ids) {
- list.add(id);
- }
- return list;
- }
- /**
- * 在原有基础上增加对象
- * @param <T>
- * @param list
- * @param ids
- * @return
- */
- public static <T> List<T> getList(List<T> list,T...ids){
- if(ids==null || ids.length<1)return list;
- List<T> resultList = new ArrayList<T>();
- resultList.addAll(list);
- for (T id : ids) {
- resultList.add(id);
- }
- return resultList;
- }
- /**
- * 获得Integer对象列表
- * @param ints
- * @return
- */
- public static List<Integer> getList(int... ints){
- List<Integer> list = new ArrayList<Integer>();
- 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 <T>
- * @param list
- * @return
- */
- public static <T> List<T> getReversionList(List<T> list) {
- Stack<T> stack = new Stack<T>();
- for(T t : list){
- stack.push(t);
- }
- return getListFromStack(stack);
- }
- /**
- * 获得List :stack转list
- * @param <T>
- * @param stack
- * @return
- */
- public static <T> List<T> getListFromStack(Stack<T> stack){
- List<T> l = new ArrayList<T>();
- 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<array.length;i++)
- array[i] = StringUtil.getInt(strings[i]);
- return array;
- }
- /**
- * 获得int数组
- * @return
- */
- public static int[] getIntArray(int...k) {
- return k;
- }
- /**
- * 返回String的Map格式:key,value,key,value
- * @param strings
- * @return
- */
- public static Map<String, String> getMap(String...strings) {
- if(strings==null || strings.length%2!=0)return Collections.emptyMap();
- Map<String,String> map = new LinkedHashMap<String,String>();
- for(int i=0;i<strings.length;i+=2){
- map.put(strings[i], strings[i+1]);
- }
- return map;
- }
- /**
- * 获得Integer数组
- * @param it
- * @return
- */
- public static Integer[] getIntegerArrayFromIterable(Iterable<?> it){
- if(it==null)return new Integer[0];
- List<Integer> list = new ArrayList<Integer>();
- 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<Integer> list = new ArrayList<Integer>();
- for(String str :strings)
- list.add(StringUtil.getInt(str));
- return list.toArray(new Integer[list.size()]);
- }
- /**
- * 获得列表中的某个索引,如果不存在返回null
- * @param <T>
- * @param list
- * @param index
- * @return
- */
- public static <T> T get(List<T> list , int index){
- if(list==null || list.size()<index-1)return null;
- return list.get(index);
- }
- /**
- * 获得按值排序的map
- * @param <K>
- * @param <V>
- * @param map
- * @param comparator 值排序器
- * @return
- */
- public static <K extends Object,V extends Comparable<?>> Map<K,V> getSortedByValueMap(final Map<K,V> map, Comparator<V> comparator) {
- if(map==null || map.isEmpty() || comparator==null)
- return map;
- Map<K,V> resultMap = new LinkedHashMap<K,V>();
- final List<V> values = new ArrayList<V>();
- values.addAll(map.values());
- Collections.sort( values, comparator);
- List<K> keys = new ArrayList<K>();
- keys.addAll(map.keySet());
- Collections.sort(keys,new Comparator<K>(){
- 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 <T>
- * @param t
- * @param def
- * @return
- */
- public static <T> 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 <T>
- * @param list0
- * @param list1
- * @return
- */
- public static <T extends Object> List<T> getExtList(List<T> list0,
- List<T> list1) {
- if(list1==null || list1.isEmpty())return list0;
- List<T> tList = new ArrayList<T>();
- 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<j;i++){
- clazz[i] = args[i].getClass();
- }
- return target.getClass().getMethod(method, clazz).invoke(target, ValidateUtil.isNullArray(args)?new Object[0]:args);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- /**
- * 动态调用类静态方法
- * @param class1
- * @param string
- * @param element
- * @return
- */
- public static Object getInvokeStaticReturn(
- Class<?> 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<j;i++){
- clazz[i] = args[i].getClass();
- }
- return targetClazz.getMethod(method, clazz).invoke(targetClazz, ValidateUtil.isNullArray(args)?new Object[0]:args);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- /**
- * 获得泛型的对象,可以设置默认值
- * @param <T>
- * @param clazz
- * @param value
- * @param t
- * @return
- */
- @SuppressWarnings("unchecked")
- public static <T> T getEnumType(Class<T> 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();
-
- }
- }
|