All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rt.core.util.RTUtil Maven / Gradle / Ivy

There is a newer version: 1.1.17
Show newest version
package com.rt.core.util;

import com.json.JSONArray;
import com.json.JSONObject;
import com.rt.core.constant.RTConst;
import com.rt.core.log.Log;
import com.rt.core.security.aes.Aes;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 常用函数
 */
public class RTUtil extends StringUtil {

    private static Log log = Log.getLog(RTUtil.class);

    public static String enparam(Map param) {
        return SecurityUtil.enparam(param);
    }

    /**
     * 清理对象内空属性.
     *
     * @param value value
     */
    public static void clearEmpty(JSONArray value) {
        if (value == null) {
            return;
        }
        for (Object obj : value) {
            if (obj instanceof JSONObject) {
                clearEmpty((JSONObject) obj);
            }
        }
    } // end fun

    /**
     * 清理对象内空属性.
     *
     * @param value value
     */
    public static void clearEmpty(JSONObject value) {
        if (value == null) {
            return;
        }
        List names = new ArrayList<>();
        for (Object o : value.keySet()) {
            Object obj = value.get(o);
            if (obj instanceof JSONObject) {
                clearEmpty((JSONObject) obj);
            } else if (obj instanceof JSONArray) {
                JSONArray a = (JSONArray) obj;
                for (Object object : a) {
                    if (object instanceof JSONObject) {
                        clearEmpty((JSONObject) object);
                    }
                }
            } else {
                if (RTUtil.isEmpty(obj)) {
                    names.add(o);
                }
            }
        }
        for (Object name : names) {
            value.remove(name);
        }
    } // end fun

    /**
     * 生成随机数字
     *
     * @param range range
     * @return int
     */
    public static int createRandomNumber(int range) {
        if (range <= 0) {
            return 0;
        }
        return RandomUtil.genNum(range + 1);
    }

    /**
     * 创建随机码 长度 = 13 + randomLength
     *
     * @param randomLength randomLength
     * @return String
     */
    public static String createRandomCode(int randomLength) {
        return RandomUtil.createRandomCode(randomLength);
    }

    /**
     * 生成随机英文字母
     *
     * @param length length
     * @return String
     */
    public static String createRandomEnglish(int length) {
        return RandomUtil.genEnglish(length);
    }

    /**
     * 加密
     *
     * @param key key
     * @param value value
     * @return byte[]
     */
    public static byte[] aesEncode(byte[] key, byte[] value) {
        return Aes.encode(key, value);
    }

    /**
     * 解密
     *
     * @param key key
     * @param value value
     * @return byte[]
     */
    public static byte[] aesDecode(byte[] key, byte[] value) {
        return Aes.decode(key, value);
    }

    /**
     * 获取数组中指定位置值
     *
     * @param array array
     * @param index index
     * @param  object
     * @return T Object
     */
    public static  T val(Object[] array, int index) {
        return val(array, index, RTConst.EMPTY);
    }

    /**
     * 获取数组中指定位置值
     *
     * @param array array
     * @param index index
     * @param defaultValue defaultValue
     * @param  object
     * @return T Object
     */
    public static  T val(Object[] array, int index, Object defaultValue) {
        if (RTUtil.isEmpty(defaultValue)) {
            defaultValue = RTConst.EMPTY;
        }
        if (array == null || array.length == 0) {
            return (T) defaultValue;
        }
        if (index < 0) {
            return (T) defaultValue;
        }
        if (index < array.length) {
            return (T) array[index];
        }
        return (T) defaultValue;
    }

    /**
     * 获取数组中指定位置值
     *
     * @param array array
     * @param index index
     * @param  object
     * @return T Object
     */
    public static  T val(List array, int index) {
        return val(array, index, null);
    }

    /**
     * 获取数组中指定位置值
     *
     * @param array array
     * @param index index
     * @param defaultValue defaultValue
     * @param  object
     * @return T object
     */
    public static  T val(List array, int index, Object defaultValue) {
        if (RTUtil.isEmpty(defaultValue)) {
            defaultValue = RTConst.EMPTY;
        }
        if (array == null || array.size() == 0) {
            return (T) defaultValue;
        }
        if (index < 0) {
            return (T) defaultValue;
        }
        if (index < array.size()) {
            return (T) array.get(index);
        }
        return (T) defaultValue;
    }

    /**
     * 删除Map中的指定属性.
     *
     * @param object   Map
     * @param keyArray keyArray
     */
    public static void removeMapObject(Map object, String[] keyArray) {
        if (keyArray == null) {
            return;
        }
        for (String aKeyArray : keyArray) {
            object.remove(aKeyArray);
        }
    }

    /**
     * 异常转换为字符串.
     *
     * @param e Throwable
     * @return String
     */
    public static String exceptionToString(Throwable e) {
        if (e == null) {
            return RTConst.EMPTY;
        }
        StringBuffer error = new StringBuffer();
        error.append(e.toString());
        for (StackTraceElement traceElement : e.getStackTrace()) {
            error.append("\t");
            error.append(traceElement.toString());
            error.append("\n");
        }
        return error.toString();
    }

    public static boolean hasClass(String className) {
        try {
            return Class.forName(className) != null;
        } catch (Throwable t) {
            return false;
        }
    }

    /**
     * 通过完整className获取对象.
     *
     * @param className className
     * @param  object
     * @return T object
     */
    public static  T getInstance(String className) {
        if (StringUtil.isEmpty(className)) {
            return null;
        }
        // 注册类对象.
        Object regClass = null;
        try {
            regClass = Class.forName(className).newInstance();
        } catch (Exception e) {
            log.error(e);
        }
        return (T) regClass;
    }

    /**
     * 通过完整className获取Class对象.
     *
     * @param className className
     * @return Object
     */
    public static Class getClass(String className) {
        if (StringUtil.isEmpty(className)) {
            return null;
        }
        // 注册类对象.
        Class clazz = null;
        try {
            clazz = Class.forName(className);
        } catch (Exception e) {
            log.error(e);
        }
        return clazz;
    }

    /**
     * 转换名称. user_name - userName
     * sex - sex
     *
     * @param colName colName
     * @return RealName
     */
    public static String dbColNameToMappingName(String colName) {
        if (StringUtil.isNotEmpty(colName)) {
            String[] real = colName.split(RTConst.DB_SPLIT);
            StringBuffer realName = new StringBuffer();
            for (int i = 0; i < real.length; i++) {
                if (i == 0) {
                    realName.append(real[i]);
                } else {
                    realName.append(real[i].substring(0, 1).toUpperCase());
                    realName.append(real[i].substring(1, real[i].length()));
                }
            }
            return realName.toString();
        }
        return colName;
    }

    /**
     * 属性名变方法名
     *
     * @param attributeName attributeName
     * @param getOrSet getOrSet
     * @return String
     */
    public static String attributeToMethodName(String attributeName, String getOrSet) {
        StringBuffer methodName = new StringBuffer();
        if (StringUtil.isNotEmpty(attributeName)) {
            methodName.append(getOrSet);
            methodName.append(attributeName.substring(0, 1).toUpperCase());
            methodName
                    .append(attributeName.substring(1, attributeName.length()));
        }
        return methodName.toString();
    }

    /**
     * 获取类中所有的变量
     *
     * @param obj obj
     * @return Map MethodMap
     */
    public static Map getMethodMap(Object obj) {
        Map methodMap = new LinkedHashMap();
        Method[] methods = obj.getClass().getMethods();
        for (Method method : methods) {
            methodMap.put(method.getName(), method);
        }
        return methodMap;
    }

    /**
     * 获得方法中第一个参数的类型
     *
     * @param methodMap methodMap
     * @param methodName methodName
     * @return Class ParameterType
     */
    public static Class getMethodFirstParameter(Map methodMap, String methodName) {
        Class[] classArray = getMethodParameter(methodMap, methodName);
        if (null != classArray && classArray.length > 0) {
            return classArray[0];
        }
        return null;
    }

    /**
     * 获得方法中第一个参数的类型
     *
     * @param  methodMap methodMap
     * @param  methodName methodName
     * @return ParameterType
     */
    public static Class[] getMethodParameter(Map methodMap, String methodName) {
        if (methodMap.containsKey(methodName)) {
            Method method = (Method) methodMap.get(methodName);
            return method.getParameterTypes();
        }
        return null;
    }

    /**
     * 获取Field
     *
     * @param obj obj
     * @return fieldMap
     */
    public static Map getFieldMap(Object obj) {
        Map fieldMap = new LinkedHashMap();
        Field[] field = obj.getClass().getFields();
        for (int i = 0; i < field.length; i++) {
            fieldMap.put(field[i].getName(), field[i]);
        }

        return fieldMap;
    }

    /**
     * 获取函数中用到的列名 转换并去掉无用的字符
     *
     * @param obj obj
     * @param replaceChar replaceChar
     * @return fieldMap
     */
    public static Map getMethodFieldMap(Object obj, String replaceChar) {
        Map fieldMap = new LinkedHashMap();
        Field[] field = obj.getClass().getFields();
        for (Field aField : field) {
            fieldMap.put(
                    aField.getName().replaceFirst(replaceChar, RTConst.EMPTY)
                            .toLowerCase(), aField);
        }
        return fieldMap;
    }

    /**
     * 执行类中的某个方法.
     *
     * @param obj obj
     * @param mothodName mothodName
     * @param parpTypes parpTypes
     * @param parp parp
     * @return Object
     * @throws Exception Exception
     */
    public static Object doMethod(Object obj, String mothodName,
                                  Class[] parpTypes, Object[] parp) throws Exception {
        return obj.getClass().getMethod(mothodName, parpTypes)
                .invoke(obj, parp);
    }

    /**
     * 将得到的数据类型转换为逻辑中需要的值.
     *
     * @param obj obj
     * @return String value
     */
    public static String convertDataForGet(Object obj) {
        String value = null;
        if (obj != null) {
            if (obj instanceof Date) {
                // 如果是日期类型需要转换成long值再toString.
                value = String.valueOf(((Date) obj).getTime());
            } else {
                // 其它情况直接toString.
                value = String.valueOf(obj);
            }
        }
        return value;
    }

    /**
     * 类型转换.
     *
     * @param obj obj
     * @param clazz clazz
     * @return Object
     */
    public static Object[] convertDataForSet(Object[] obj, Class[] clazz) {
        for (int i = 0; i < obj.length; i++) {
            String type = clazz[i].getName();
            if (type.equals(Date.class.getName())) {
                // 如果是日期类型需要将lang转换成Calendar再得到Date类型.
                Calendar cal = Calendar.getInstance();
                cal.setTimeInMillis(Long.parseLong(obj[i].toString()));
                obj[i] = cal.getTime();
            } else if (type.equals(BigDecimal.class.getName())) {
                // BigDecimal.
                obj[i] = new BigDecimal(obj[i].toString());
            } else if (type.equals(Integer.class.getName())) {
                // Integer
                obj[i] = new Integer(obj[i].toString());
            } else if (type.equals(Long.class.getName())) {
                // Long
                obj[i] = new Long(obj[i].toString());
            } else if (type.equals(Byte.class.getName())) {
                // Byte
                obj[i] = new Byte(obj[i].toString());
            }
        }
        // 其它情况视为String 原样返回,无需处理.
        return obj;
    }

    /**
     * Map to List
     *
     * @param map map
     * @return List
     */
    public static List mapToList(Map map) {
        List list = new ArrayList();

        if (map != null) {
            for (Object object : map.keySet()) {
                list.add(map.get(object));
            }
        }

        return list;
    }

    /**
     * arrayToList
     *
     * @param array array
     * @return List
     */
    public static List arrayToList(Object[] array) {
        List list = new JSONArray();
        if (array == null) {
            return list;
        }
        Collections.addAll(list, array);
        return list;
    }

    /**
     * arrayToList
     *
     * @param  array array
     * @return List
     */
    public static JSONArray arrayToJSONArray(Object[] array) {
        JSONArray list = new JSONArray();
        if (array == null) {
            return list;
        }
        Collections.addAll(list, array);
        return list;
    }

    /**
     * arrayToList
     *
     * @param array array
     * @return List
     */
    public static List arrayToList(int[] array) {
        List list = new JSONArray();
        if (array == null) {
            return list;
        }
        for (int anArray : array) {
            list.add(anArray);
        }
        return list;
    }

    /**
     * arrayToList
     *
     * @param array array
     * @return List
     */
    public static List arrayToList(double[] array) {
        List list = new JSONArray();
        if (array == null) {
            return list;
        }
        for (double anArray : array) {
            list.add(BigDecimal.valueOf(anArray).toPlainString());
        }
        return list;
    }

    /**
     * 替换字符串中的参数
     *
     * @param varStr    demo:hello {0}
     * @param propertie String[]
     * @return String
     */
    public static String replaceProperties(String varStr, String propertie) {
        if (isEmpty(varStr) || isEmpty(propertie)) {
            return varStr;
        }
        varStr = varStr.replaceFirst(RTConst.PROPERTIE, propertie);
        return varStr;
    }

    /**
     * 替换字符串中的参数
     *
     * @param varStr    demo:hello {0}
     * @param propertie String[]
     * @return String
     */
    public static String replaceProperties(String varStr, String[] propertie) {
        if (isEmpty(varStr) || isEmpty(propertie)) {
            return varStr;
        }
        for (String aPropertie : propertie) {
            varStr = replaceProperties(varStr, aPropertie);
        }
        return varStr;
    }

    /**
     * 替换字符串中的变量
     *
     * @param varStr varStr
     * @param value value
     * @return String
     */
    public static String replaceVariable(String varStr, String value) {
        if (varStr == null) {
            return null;
        }
        return varStr.replaceFirst(RTConst.VARIABLE, value);
    }

    /**
     * 字符串转为变量写法 name - ${name}
     *
     * @param name name
     * @return String
     */
    public static String toVariable(String name) {
        return new StringBuffer("${").append(name).append("}").toString();
    }

    /**
     * 转换表达式中变量
     *
     * @param expression expression
     * @param variableMap variableMap
     * @return expression
     */
    public static String replaceVariable(String expression, Map variableMap) {
        if (RTUtil.isEmpty(expression)) {
            return null;
        }
        if (variableMap == null) {
            return expression;
        }
        List var = matcherList(RTConst.VARIABLE, expression, 1);
        for (Object key : var) {
            String code = RTUtil.val(variableMap.get(key));
            if (RTUtil.isEmpty(code)) {
                code = RTUtil.val(key);
            }
            expression = expression.replaceFirst(RTConst.VARIABLE, code);
        }
        return expression;
    }

    /**
     * 转换表达式中变量
     *
     * @param expression expression
     * @param variablePrefix variablePrefix
     * @param variableMap variableMap
     * @return expression
     */
    public static String replaceVariable(String expression,
                                         String variablePrefix, Map variableMap) {
        if (RTUtil.isEmpty(expression)) {
            return null;
        }
        if (variableMap == null) {
            return expression;
        }
        List var = matcherList(RTConst.VARIABLE, expression, 1);
        for (Object key : var) {
            String code = RTUtil.val(variableMap.get(key));
            if (RTUtil.isEmpty(code)) {
                code = RTUtil.val(key);
            }
            expression = expression.replaceFirst(RTConst.VARIABLE,
                    variablePrefix + code);
        }
        return expression;
    }

    /**
     * 切割字符串
     *
     * @param splitValue splitValue
     * @return List
     */
    public static List splitToList(String splitValue) {
        return splitToList(splitValue, RTConst.COMMA);
    }

    /**
     * 切割字符串
     *
     * @param str str
     * @param split split
     * @return List
     */
    public static List splitToList(String str, String split) {
        String[] p = null;
        try {
            p = str.split(split);
        } catch (Exception e) {
            return new JSONArray();
        }
        return arrayToList(p);
    }

    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 对输入字符串进行SHA1加密
     *
     * @param str 输入串
     * @return 加密后的字符串
     */
    public static String getMD5Value(String str) {
        return SecurityUtil.getMD5Value(str);
    }

    /**
     * 对输入字符串进行SHA1加密
     *
     * @param str 输入串
     * @return 加密后的字符串
     */
    public static String getSHA1Value(String str) {
        return SecurityUtil.getSHA1Value(str);
    }

    /**
     * DES 加密 返回16进制字符串
     *
     * @param key key
     * @param str hex16
     * @return String
     */
    public static String encryptDesByHex(String key, String str) {
        return SecurityUtil.encryptDesByHex(key, str);
    }

    /**
     * DES 16进制字符串解密
     *
     * @param key key
     * @param code hex16
     * @return String
     */
    public static String decryptDesByHex(String key, String code) {
        return SecurityUtil.decryptDesByHex(key, code);
    }

    /**
     * 格式化数字
     *
     * @param number number
     * @param format format
     * @return String
     */
    public static String format(BigDecimal number, String format) {
        return NumberUtil.format(number, format);
    }

    /**
     * 格式化数字
     *
     * @param number number
     * @param format format
     * @return String
     */
    public static String format(double number, String format) {
        return NumberUtil.format(number, format);
    }

    /**
     * 格式化数字
     *
     * @param number number
     * @param format format
     * @return String
     */
    public static String format(String number, String format) {
        return NumberUtil.format(number, format);
    }

    /**
     * 格式化数值
     *
     * @param val val
     * @param newScale newScale
     * @return double
     */
    public static double format(double val, int newScale) {
        return NumberUtil.format(val, newScale);
    }

    /**
     * 格式化数值
     * BigDecimal.ROUND_DOWN 直接删除多余的小数位,如2.35会变成2.3
     * BigDecimal.ROUND_UP 进位处理,2.31变成2.4
     * BigDecimal.ROUND_HALF_UP 四舍五入,2.35变成2.4
     * BigDecimal.ROUND_HALF_DOWN 四舍五入,2.35变成2.3,如果是5则向下舍
     *
     * @param val val
     * @param newScale newScale
     * @param roundingMode roundingMode
     * @return double
     */
    public static double format(String val, int newScale, int roundingMode) {
        return NumberUtil.format(val, newScale, roundingMode);
    }

    /**
     * 格式化数值
     * BigDecimal.ROUND_DOWN 直接删除多余的小数位,如2.35会变成2.3
     * BigDecimal.ROUND_UP 进位处理,2.35变成2.4
     * BigDecimal.ROUND_HALF_UP 四舍五入,2.35变成2.4
     * BigDecimal.ROUND_HALF_DOWN 四舍五入,2.35变成2.3,如果是5则向下舍
     *
     * @param val val
     * @param newScale newScale
     * @param roundingMode roundingMode
     * @return double
     */
    public static double format(double val, int newScale, int roundingMode) {
        return NumberUtil.format(val, newScale, roundingMode);
    }

    /**
     * 除法 默认四舍五入
     *
     * @param divisor divisor
     * @param dividend dividend
     * @param defaultValue defaultValue
     * @return BigDecimal
     */
    public static BigDecimal divide(BigDecimal divisor, BigDecimal dividend,
                                    BigDecimal defaultValue) {
        return NumberUtil.divide(divisor, dividend, defaultValue);
    }

    /**
     * 除法 默认四舍五入
     *
     * @param divisor divisor
     * @param dividend dividend
     * @param scale scale
     * @param defaultValue defaultValue
     * @return BigDecimal
     */
    public static BigDecimal divide(BigDecimal divisor, BigDecimal dividend,
                                    int scale, BigDecimal defaultValue) {
        return NumberUtil.divide(divisor, dividend, scale, defaultValue);
    }

    /**
     * 除法 默认四舍五入
     *
     * @param divisor divisor
     * @param dividend dividend
     * @return BigDecimal
     */
    public static BigDecimal divide(BigDecimal divisor, BigDecimal dividend) {
        return NumberUtil.divide(divisor, dividend);
    }

    /**
     * 除法 默认四舍五入
     *
     * @param divisor divisor
     * @param dividend dividend
     * @param scale scale
     * @return BigDecimal
     */
    public static BigDecimal divide(BigDecimal divisor, BigDecimal dividend,
                                    int scale) {
        return NumberUtil.divide(divisor, dividend, scale);
    }

    /**
     * 填充
     *
     * @param length length
     * @param defaultValue defaultValue
     * @return BigDecimal[]
     */
    public static BigDecimal[] fill(int length, BigDecimal defaultValue) {
        return NumberUtil.fill(length, defaultValue);
    }

    /**
     * 填充
     *
     * @param length length
     * @param defaultValue defaultValue
     * @return Integer[]
     */
    public static Integer[] fill(int length, Integer defaultValue) {
        return NumberUtil.fill(length, defaultValue);
    }

    /**
     * 求N个数的最大值
     *
     * @param array array
     * @return BigDecimal
     */
    public static Object max(Object[] array) {
        return NumberUtil.max(array);
    }

    /**
     * 求N个数的最大值
     *
     * @param array array
     * @return BigDecimal
     */
    public static int max(int[] array) {
        return NumberUtil.max(array);
    }

    // end number util

    // start el util

    /**
     * 执行普通表达式,表达式中不能包含未放入堆栈的对象.
     *
     * @param expression expression
     * @param elContext elContext
     * @return Object
     * @throws Exception Exception
     */
    public static Object elFindValue(String expression, Object elContext)
            throws Exception {
        return ELUtil.findValue(expression, elContext);
    }

    /**
     * 执行ognl表达式
     *
     * @param expression expression
     * @param elContext elContext
     * @return String
     * @throws Exception Exception
     */
    public static String elFindString(String expression, Object elContext)
            throws Exception {
        return ELUtil.findString(expression, elContext);
    }

    /**
     * 执行ognl表达式
     *
     * @param expression expression
     * @param props props
     * @param elContext elContext
     * @return Object
     * @throws Exception Exception
     */
    public static Object elFindValue(String expression, Map props,
                                     Object elContext) throws Exception {
        return ELUtil.findValue(expression, props, elContext);

    }

    /**
     * 取得ELContext
     *
     * @return ELContext
     */
    public static Map getElContext() {
        return ELUtil.getElContext();
    }

    // end el util

    // start time util

    /**
     * 获得当前时间Calendar格式
     *
     * @param date Date
     * @return Calendar
     */
    public static Calendar getCalendar(Date date) {
        return TimeUtil.getCalendar(date);
    }

    /**
     * 获得当前时间Calendar格式
     *
     * @param longDate longDate
     * @return Calendar
     */
    public static Calendar getCalendar(long longDate) {
        return TimeUtil.getCalendar(longDate);
    }

    /**
     * 得到当前时间
     *
     * @return Date
     */
    public static Date getDate() {
        return TimeUtil.getDate();
    }

    /**
     * 得到指定格式的日期字符串
     *
     * @param format format
     * @return nowDate String.
     */
    public static String getDateToString(String format) {
        return TimeUtil.getDateToString(format);
    }

    /**
     * 得到当前时间的毫秒部分
     *
     * @return String SSS
     */
    public static String getDateToSecond() {
        return TimeUtil.getDateToSecond();
    }

    /**
     * 转换现在的时间为Long格式 以毫秒为单位计算,从1970年1月1日0时0分0秒0000毫秒算起.
     *
     * @return longDate long
     */
    public static long getDateTolong() {
        return TimeUtil.getDateTolong();
    }

    /**
     * @return longDate Long
     */
    public static Long getDateToLong() {
        return TimeUtil.getDateToLong();
    }

    /**
     * this.dateTolong
     *
     * @return longDate string
     */
    public static String getDateToString() {
        return TimeUtil.getDateToString();
    }

    /**
     * long转换为日期
     *
     * @param longDate longDate
     * @return Date
     */
    public static Date toDate(String longDate) {
        return TimeUtil.toDate(longDate);
    }

    /**
     * long转换为日期
     *
     * @param longDate longDate
     * @return Date
     */
    public static Date toDate(long longDate) {
        return TimeUtil.toDate(longDate);
    }

    /**
     * long转换为日期
     *
     * @param longDate longDate
     * @return Date
     */
    public static Date toDate(Long longDate) {
        return TimeUtil.toDate(longDate);
    }

    /**
     * String到Date格式的转换
     *
     * @param dateString like 1970/01/01
     * @param format format
     * @return date Date
     */
    public static Date toDate(String dateString, String format) {
        return TimeUtil.toDate(dateString, format);
    }

    /**
     * date到String格式的转换
     *
     * @param date date
     * @param format format
     * @return date String
     */
    public static String toStringDate(Date date, String format) {
        return TimeUtil.toStringDate(date, format);
    }

    /**
     * stringDate转为long型
     *
     * @param stringDate stringDate
     * @param format format
     * @return long
     */
    public static long tolongDate(String stringDate, String format) {
        return TimeUtil.tolongDate(stringDate, format);
    }

    /**
     * stringDate转为long型
     *
     * @param stringDate stringDate
     * @param format format
     * @return Long
     */
    public static Long toLongDate(String stringDate, String format) {
        return TimeUtil.toLongDate(stringDate, format);
    }

    /**
     * long or integer型转换成date string
     *
     * @param longDate longDate
     * @param format format
     * @return StringDate
     */
    public static String toStringDate(String longDate, String format) {
        return TimeUtil.toStringDate(longDate, format);
    }

    /**
     * long or integer型转换成date string
     *
     * @param longDate longDate
     * @param format format
     * @return StringDate
     */
    public static String toStringDate(long longDate, String format) {
        return TimeUtil.toStringDate(longDate, format);
    }

    /**
     * long or integer型转换成date string
     *
     * @param longDate longDate
     * @param format format
     * @return StringDate
     */
    public static String toStringDate(Long longDate, String format) {
        return TimeUtil.toStringDate(longDate, format);
    }

    /**
     * 取得某月最后一天的日期整数值
     *
     * @param year  年(yyyy)
     * @param month 月(mm或m)
     * @return int 日期整数值
     */
    public static int getMaxDayOfMonth(String year, String month) {
        return TimeUtil.getMaxDayOfMonth(year, month);
    }

    /**
     * 按生日转换成年龄
     *
     * @param birthday birthday
     * @return int
     */
    public static int getAgeOfBirthday(String birthday) {
        return TimeUtil.getAgeOfBirthday(birthday);
    }

    /**
     * 按生日转换成年龄
     *
     * @param birthday birthday
     * @return int
     */
    public static int getAgeOfBirthday(Date birthday) {
        return TimeUtil.getAgeOfBirthday(birthday);
    }

    // end time util

    /**
     * 获取根路径
     *
     * @return String
     */
    public static String getUrlPath() {
        try {
            return RTUtil.class.getResource(RTConst.ROOT_PATH).getPath();
        } catch (Exception e) {
            log.error(e);
            return null;
        }
    }

    /**
     * 获取根路径
     *
     * @param url url
     * @return java.net.URL
     */
    public static java.net.URL getUrl(String url) {
        if (url == null) {
            return null;
        }
        try {
            return RTUtil.class.getResource(url);
        } catch (Exception e) {
            log.error(e);
            return null;
        }
    }


    /**
     * 修正时间 格式转换 时区处理
     *
     * @param timeZone  like +0800
     * @param isGetTime isGetTime
     * @return long
     */
    public static long fixTimeZone(String timeZone, boolean isGetTime) {
        return fixTimeZone(getDateToLong(), timeZone, isGetTime);
    }

    /**
     * 获取修正时间
     *
     * @param time      now time
     * @param timeZone  like +0800
     * @param isGetTime get or set time.
     * @return long fix long time
     */
    public static long fixTimeZone(final long time,
                                   final String timeZone, boolean isGetTime) {
        if (isEmpty(timeZone) || timeZone.length() != 5) {
            return time;
        }
        if (equals(getDateToString("Z"), timeZone)) {
            return time;
        }

        String f = substring(timeZone, 0, 1);
        if (equals(RTConst.SPACE, f)) {
            f = "+";
        }
        // 3600000 是小时的毫秒值
        long h = tolong(substring(timeZone, 1, 3)) * 3600000;
        // 60000 是分钟的毫秒值
        long m = tolong(substring(timeZone, 3, 5)) * 60000;
        // 如果是保存时间 需要取反
        if (!isGetTime) {
            if (RTConst.ADD.equals(f)) {
                f = RTConst.LINE;
            } else {
                f = RTConst.ADD;
            }
        }
        return time + tolong(f + (h + m));
    }

}