IUtils日期工具&数据类型转换工具&通用判空工具

发布时间 2023-03-25 00:43:07作者: yub4by
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * dhb
 * 时间工具类
 */
public class IDate {
    public static final int MINUTE = 1;
    public static final int HOUR = 2;
    public static final int MINUTE_SECOND = 3;
    public static final int HOUR_MINUTE_SECOND = 3;

    public IDate() {
    }

    public static int daysBetween(Date d1, Date d2) {
        new SimpleDateFormat("yyyy-MM-dd");
        long l2 = d2.getTime();
        long l1 = d1.getTime();
        return IConvert.long2int((l2 - l1) / 86400000L);
    }

    public static Date getStartDateTime(Date date) {
        try {
            Calendar cal = Calendar.getInstance();
            if (date != null) {
                cal.setTime(date);
            }

            cal.set(11, 0);
            cal.set(12, 0);
            cal.set(13, 0);
            cal.set(14, 0);
            return cal.getTime();
        } catch (Exception var2) {
            var2.printStackTrace();
            return null;
        }
    }

    public static Date getEndDateTime(Date date) {
        try {
            Calendar cal = Calendar.getInstance();
            if (date != null) {
                cal.setTime(date);
            }

            cal.set(11, 23);
            cal.set(12, 59);
            cal.set(13, 59);
            cal.set(14, 999);
            return cal.getTime();
        } catch (Exception var2) {
            var2.printStackTrace();
            return null;
        }
    }

    public static Date getMonthFirstDay(Date date, Boolean hourFlag) {
        try {
            Calendar cal = Calendar.getInstance();
            if (date != null) {
                cal.setTime(date);
            }

            cal.set(5, 1);
            if (hourFlag) {
                cal.set(11, 0);
                cal.set(12, 0);
                cal.set(13, 0);
            }

            return cal.getTime();
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static Date getMonthLastDay(Date date, Boolean hourFlag) {
        try {
            Calendar cal = Calendar.getInstance();
            if (date != null) {
                cal.setTime(date);
            }

            cal.add(2, 1);
            cal.set(5, 0);
            if (hourFlag) {
                cal.set(11, 23);
                cal.set(12, 59);
                cal.set(13, 59);
            }

            return cal.getTime();
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        c.set(11, 0);
        c.set(12, 0);
        c.set(13, 0);
        c.set(14, 0);
        Long today = c.getTimeInMillis() / 1000L;
        System.out.println(date2string(new Date(c.getTimeInMillis())));
    }

    public static int getMonthIndex(Date date) {
        try {
            Calendar cal = Calendar.getInstance();
            if (date != null) {
                cal.setTime(date);
            }

            return cal.get(2) + 1;
        } catch (Exception var2) {
            var2.printStackTrace();
            return 0;
        }
    }

    public static int getYearIndex(Date date) {
        try {
            Calendar cal = Calendar.getInstance();
            if (date != null) {
                cal.setTime(date);
            }

            return cal.get(1);
        } catch (Exception var2) {
            var2.printStackTrace();
            return 0;
        }
    }

    public static int getDayIndex(Date date) {
        try {
            Calendar cal = Calendar.getInstance();
            if (date != null) {
                cal.setTime(date);
            }

            return cal.get(5);
        } catch (Exception var2) {
            var2.printStackTrace();
            return 0;
        }
    }

    public static Boolean isDate(String s) {
        try {
            return ICommons.isNullOrEmpty(string2date(s)) ? false : true;
        } catch (Exception var2) {
            var2.printStackTrace();
            return false;
        }
    }

    public static Time data2time(Date date) {
        if (date == null) {
            date = Calendar.getInstance().getTime();
        }

        try {
            return Time.valueOf(iSimpleDateFormat("HH:mm:ss", date));
        } catch (Exception var2) {
            var2.printStackTrace();
            return null;
        }
    }

    public static Timestamp date2timestamp(Date date) {
        if (ICommons.isNullOrEmpty(date)) {
            return null;
        } else {
            try {
                return Timestamp.valueOf(iSimpleDateFormat("yyyy-MM-dd HH:mm:ss", date));
            } catch (Exception var2) {
                var2.printStackTrace();
                return null;
            }
        }
    }

    public static Timestamp string2timestamp(String s) {
        return string2timestamp(s, IDate.IPattern.ilong);
    }

    public static Timestamp string2timestamp(String s, IDate.IPattern pattern) {
        Date date = string2date(s, pattern);
        return date == null ? null : date2timestamp(date);
    }

    public static Date timestamp2date(Timestamp ts) {
        return new Date(ts.getTime());
    }

    public static String date2string(Timestamp timestamp) {
        return date2string(timestamp, IDate.IPattern.ilong);
    }

    public static String date2string(Timestamp timestamp, Boolean addOneDay) {
        return date2string(timestamp, IDate.IPattern.ilong, addOneDay);
    }

    public static String date2string(Timestamp timestamp, IDate.IPattern pattern) {
        return date2string(timestamp, pattern, false);
    }

    public static String date2string(Timestamp timestamp, IDate.IPattern pattern, Boolean addOneDay) {
        if (pattern == IDate.IPattern.ilong) {
            try {
                return date2string(timestamp, "yyyy-MM-dd HH:mm:ss", addOneDay);
            } catch (Exception var5) {
                var5.printStackTrace();
            }
        }

        if (pattern == IDate.IPattern.ishort) {
            try {
                return date2string(timestamp, "yyyy-MM-dd", addOneDay);
            } catch (Exception var4) {
                var4.printStackTrace();
            }
        }

        return null;
    }

    public static String date2string(Timestamp timestamp, String pattern) {
        return date2string(timestamp, pattern, false);
    }

    public static String date2string(Timestamp timestamp, String pattern, Boolean addOneDay) {
        try {
            return addOneDay ? iSimpleDateFormat(pattern, addOneDay(timestamp)) : iSimpleDateFormat(pattern, (Date)timestamp);
        } catch (Exception var4) {
            var4.printStackTrace();
            return null;
        }
    }

    public static String date2string(Date date) {
        return date2string(date, false);
    }

    public static String date2string(Date date, Boolean addOneDay) {
        return date2string(date, IDate.IPattern.ilong, addOneDay);
    }

    public static String date2string(Date date, IDate.IPattern pattern) {
        return date2string(date, pattern, false);
    }

    public static String date2string(Date date, IDate.IPattern pattern, Boolean addOneDay) {
        if (pattern == IDate.IPattern.ilong) {
            try {
                return date2string(date, "yyyy-MM-dd HH:mm:ss", addOneDay);
            } catch (Exception var5) {
                var5.printStackTrace();
            }
        }

        if (pattern == IDate.IPattern.ishort) {
            try {
                return date2string(date, "yyyy-MM-dd", addOneDay);
            } catch (Exception var4) {
                var4.printStackTrace();
            }
        }

        return null;
    }

    public static String date2string(Date date, String pattern) {
        return date2string(date, pattern, false);
    }

    public static String date2string(Date date, String pattern, Boolean addOneDay) {
        try {
            return addOneDay ? iSimpleDateFormat(pattern, addOneDay(date)) : iSimpleDateFormat(pattern, date);
        } catch (Exception var4) {
            var4.printStackTrace();
            return null;
        }
    }

    public static Date string2date(String s) {
        return string2date(s, IDate.IPattern.ilong);
    }

    public static Date string2date(String s, IDate.IPattern pattern) {
        if (pattern == IDate.IPattern.ilong) {
            return string2date(s, "yyyy-MM-dd HH:mm:ss");
        } else {
            return pattern == IDate.IPattern.ishort ? string2date(s, "yyyy-MM-dd") : null;
        }
    }

    public static Date string2date(String s, String pattern) {
        if (!ICommons.isNullOrEmpty(s) && !ICommons.isNullOrEmpty(pattern)) {
            try {
                return iSimpleDateFormat(pattern, s);
            } catch (Exception var3) {
                var3.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    public static Date addYear(Date date, Integer i) {
        if (date == null) {
            date = new Date();
        }

        if (ICommons.isNullOrEmpty(i)) {
            i = 1;
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(1, i);
        return cal.getTime();
    }

    public static Date addYearNow(Integer i) {
        return addYear(new Date(), i);
    }

    public static Date addYearNowOne() {
        return addYear(new Date(), 1);
    }

    public static Date addMonth(Date date, Integer i) {
        if (date == null) {
            date = new Date();
        }

        if (ICommons.isNullOrEmpty(i)) {
            i = 1;
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(2, i);
        return cal.getTime();
    }

    public static Date addMonthNow(Integer i) {
        return addMonth(new Date(), i);
    }

    public static Date addMonthNowOne() {
        return addMonth(new Date(), 1);
    }

    public static Date addOneDay(Date date) {
        return addDay(date, 1);
    }

    public static Date addOneDay(Timestamp ts) {
        return addDay(timestamp2date(ts), 1);
    }

    public static Date addDay(Date date, Integer i) {
        if (date == null) {
            date = new Date();
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(5, i);
        return cal.getTime();
    }

    public static Timestamp now() {
        return date2timestamp(Calendar.getInstance().getTime());
    }

    public static Long date2long() {
        return date2long(Calendar.getInstance().getTime());
    }

    public static Long date2long(Date date) {
        return IConvert.string2long(date2string(date, "yyyyMMddHHmmss"));
    }

    public static Date long2date(Long l) {
        return string2date(l.toString(), "yyyyMMddHHmmss");
    }

    private static Date iSimpleDateFormat(String pattern, String s) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            format.setLenient(false);
            return format.parse(s);
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }

    private static String iSimpleDateFormat(String pattern, Date date) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            format.setLenient(false);
            return format.format(date);
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static enum IPattern {
        ishort,
        ilong;

        private IPattern() {
        }
    }
}

import java.math.BigDecimal;

public class IConvert {
    public IConvert() {
    }

    public static Double doubleRoundHalfUp(Double d, int precision) {
        return doubleRound(d, precision, 4);
    }

    public static Double doubleRoundDown(Double d, int precision) {
        return doubleRound(d, precision, 1);
    }

    public static Double doubleRound(Double d, int precision, int roundType) {
        return (new BigDecimal(d)).setScale(precision, roundType).doubleValue();
    }

    public static String numToCnUpper(int si) {
        String[] aa = new String[]{"", "十", "百", "千", "万", "十万", "百万", "千万", "亿", "十亿"};
        String[] bb = new String[]{"一", "二", "三", "四", "五", "六", "七", "八", "九"};
        char[] ch = String.valueOf(si).toCharArray();
        int maxindex = ch.length;
        StringBuffer sb = new StringBuffer();
        int i;
        int j;
        if (maxindex == 2) {
            i = maxindex - 1;

            for(j = 0; i >= 0; ++j) {
                if (ch[j] != '0') {
                    if (j == 0 && ch[j] == '1') {
                        sb.append(aa[i]);
                    } else {
                        sb.append(bb[ch[j] - 49] + aa[i]);
                    }
                }

                --i;
            }
        } else {
            i = maxindex - 1;

            for(j = 0; i >= 0; ++j) {
                if (ch[j] != '0') {
                    sb.append(bb[ch[j] - 49] + aa[i]);
                }

                --i;
            }
        }

        return sb.toString();
    }

    public static Long[] string2LongArray(String ids) {
        try {
            String[] idsArray = ids.split(",");
            Long[] longArray = new Long[idsArray.length];

            for(int i = 0; i < idsArray.length; ++i) {
                longArray[i] = string2long(idsArray[i].trim());
            }

            return longArray;
        } catch (Exception var4) {
            var4.printStackTrace();
            return null;
        }
    }

    public static String int2string(Integer i) {
        return i == null ? null : i.toString();
    }

    public static String object2string(Object obj) {
        return ICommons.isNullOrEmpty(obj) ? "" : obj.toString();
    }

    public static Short int2short(Integer i) {
        if (i == null) {
            return null;
        } else {
            try {
                return i.shortValue();
            } catch (Exception var2) {
                return null;
            }
        }
    }

    public static Double int2double(Integer i) {
        if (i == null) {
            return 0.0D;
        } else {
            try {
                return i.doubleValue();
            } catch (Exception var2) {
                return 0.0D;
            }
        }
    }

    public static Long int2long(Integer i) {
        if (i == null) {
            return 0L;
        } else {
            try {
                return i.longValue();
            } catch (Exception var2) {
                return 0L;
            }
        }
    }

    public static Integer integerFormat(Integer i) {
        return i == null ? 0 : i;
    }

    public static Long longFormat(Long l) {
        return l == null ? 0L : l;
    }

    public static Double doubleFormat(Double d) {
        return d == null ? 0.0D : d;
    }

    public static Integer long2int(Long l) {
        if (ICommons.isNullOrEmpty(l)) {
            return 0;
        } else {
            try {
                return Long.valueOf(l).intValue();
            } catch (Exception var2) {
                var2.printStackTrace();
                return 0;
            }
        }
    }

    public static Double long2double(Long l) {
        if (ICommons.isNullOrEmpty(l)) {
            return 0.0D;
        } else {
            try {
                return l.doubleValue();
            } catch (Exception var2) {
                var2.printStackTrace();
                return 0.0D;
            }
        }
    }

    public static Integer double2int(Double d) {
        if (ICommons.isNullOrEmpty(d)) {
            return 0;
        } else {
            try {
                return Double.valueOf(d).intValue();
            } catch (Exception var2) {
                return 0;
            }
        }
    }

    public static Float double2float(Double d) {
        if (ICommons.isNullOrEmpty(d)) {
            return 0.0F;
        } else {
            try {
                return Double.valueOf(d).floatValue();
            } catch (Exception var2) {
                return 0.0F;
            }
        }
    }

    public static Integer float2int(Float f) {
        if (ICommons.isNullOrEmpty(f)) {
            return 0;
        } else {
            try {
                return f.intValue();
            } catch (Exception var2) {
                return 0;
            }
        }
    }

    public static Float object2float(Object obj) {
        if (ICommons.isNullOrEmpty(obj)) {
            return 0.0F;
        } else {
            try {
                return Float.parseFloat(object2string(obj));
            } catch (Exception var2) {
                var2.printStackTrace();
                return 0.0F;
            }
        }
    }

    public static Boolean isNumeric(String s) {
        try {
            Double.parseDouble(s);
            return true;
        } catch (Exception var2) {
            return false;
        }
    }

    public static Double string2double(String s) {
        if (ICommons.isNullOrEmpty(s)) {
            return 0.0D;
        } else {
            try {
                return Double.parseDouble(s);
            } catch (Exception var2) {
                return 0.0D;
            }
        }
    }

    public static Integer string2int(String s) {
        if (ICommons.isNullOrEmpty(s)) {
            return 0;
        } else {
            try {
                return Integer.parseInt(s);
            } catch (Exception var2) {
                var2.printStackTrace();
                return 0;
            }
        }
    }

    public static Integer object2int(Object obj) {
        if (ICommons.isNullOrEmpty(obj)) {
            return 0;
        } else {
            try {
                return Integer.parseInt(obj.toString());
            } catch (Exception var2) {
                return 0;
            }
        }
    }

    public static Long object2long(Object obj) {
        if (ICommons.isNullOrEmpty(obj)) {
            return 0L;
        } else {
            try {
                return Long.parseLong(obj.toString().trim());
            } catch (Exception var2) {
                return 0L;
            }
        }
    }

    public static Long string2long(String s) {
        if (ICommons.isNullOrEmpty(s)) {
            return 0L;
        } else {
            try {
                return Long.parseLong(s);
            } catch (Exception var2) {
                return 0L;
            }
        }
    }

    public static Double object2double(Object obj) {
        if (ICommons.isNullOrEmpty(obj)) {
            return 0.0D;
        } else {
            try {
                return Double.parseDouble(obj.toString());
            } catch (Exception var2) {
                var2.printStackTrace();
                return 0.0D;
            }
        }
    }

    public static Boolean string2boolean(String s) {
        return !ICommons.isNullOrEmpty(s) && s.trim().toLowerCase().equals("true") ? true : false;
    }
}

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ICommons {
    public ICommons() {
    }

    public static boolean isNullOrEmpty(String s) {
        return s == null || s.trim().equals("");
    }

    public static boolean isNullOrEmpty(Date date) {
        return date == null;
    }

    public static boolean isNullOrEmpty(Object obj) {
        return obj == null ? true : isNullOrEmpty(obj.toString());
    }

    public static boolean isNullOrEmpty(Long l) {
        return isNullOrEmpty(l, true);
    }

    public static boolean isNullOrEmpty(Long l, Boolean judgeZero) {
        if (l == null) {
            return true;
        } else {
            return judgeZero && l == 0L;
        }
    }

    public static boolean isNullOrEmpty(Integer i) {
        return isNullOrEmpty(i, true);
    }

    public static boolean isNullOrEmpty(Integer i, Boolean judgeZero) {
        if (i == null) {
            return true;
        } else {
            return judgeZero && i == 0;
        }
    }

    public static boolean isNullOrEmpty(Double d) {
        return isNullOrEmpty(d, true);
    }

    public static boolean isNullOrEmpty(Double d, Boolean judgeZero) {
        if (d != null && d != 0.0D) {
            return judgeZero && d == 0.0D;
        } else {
            return true;
        }
    }

    public static boolean isNullOrEmpty(List l) {
        return l == null || l.size() == 0;
    }

    public static Boolean isNullOrEmpty(Map map) {
        return map != null && map.size() != 0 ? false : true;
    }

    public static String deCode(String s) {
        try {
            return URLDecoder.decode(s, "UTF-8");
        } catch (UnsupportedEncodingException var2) {
            return null;
        }
    }
}