日期类的一些处理小结

public class DateUtil {
    private static final String formatString = "yyyy-MM-dd HH:mm:ss SSS";
    private static final String formatStringScd = "yyyy-MM-dd HH:mm:ss";
    private static final String formatStringMi = "yyyy-MM-dd HH:mm";
    private static final String formatStringHr = "yyyy-MM-dd HH";
    private static final String formatStringDy = "yyyy-MM-dd";
    private static final SimpleDateFormat formatStrScd = new SimpleDateFormat(
            "HH:mm");
    private static SimpleDateFormat formaterFull = new SimpleDateFormat(formatString);
    private static SimpleDateFormat formaterScd = new SimpleDateFormat(formatStringScd);
    private static SimpleDateFormat formaterMi = new SimpleDateFormat(formatStringMi);
    private static SimpleDateFormat formaterHr = new SimpleDateFormat(formatStringHr);
    private static SimpleDateFormat formaterDy = new SimpleDateFormat(formatStringDy);

    public synchronized static String format(Date date) {

        return formaterFull.format(date);
    }

    public synchronized static String format(Date date, String format) {
        try {
            SimpleDateFormat formater = getFormater(format);
            return formater.format(date);
        } catch (Exception ex) {
            return null;
        }
    }

    public synchronized static Date parse(String date) {
        try {
            return formaterFull.parse(date);
        } catch (Exception ex) {
            try {
                return formaterScd.parse(date);
            } catch (Exception sEx) {
                try {
                    return formaterMi.parse(date);
                } catch (Exception mEx) {
                    try {
                        return formaterHr.parse(date);
                    } catch (Exception hEx) {
                        try {
                            return formaterDy.parse(date);
                        } catch (Exception dEx) {
                            return null;
                        }
                    }
                }
            }
        }
    }

    public synchronized static Date parse(String date, String formatString) {
        try {
            return getFormater(formatString).parse(date);
        } catch (Exception ex) {
            return null;
        }
    }

    private static Map<String, SimpleDateFormat> mapFormat = new HashMap<String, SimpleDateFormat>();

    private synchronized static SimpleDateFormat getFormater(String formatString) {
        if (mapFormat.containsKey(formatString)) {
            return mapFormat.get(formatString);
        } else {
            SimpleDateFormat formater = new SimpleDateFormat(formatString);
            mapFormat.put(formatString, formater);
            return formater;
        }
    }

    public static Date addSecond(Date src, Integer value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(src);
        calendar.add(Calendar.SECOND, value);
        src = calendar.getTime();
        return src;
    }

    public static Date addMinute(Date src, Integer value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(src);
        calendar.add(Calendar.MINUTE, value);
        src = calendar.getTime();
        return src;
    }

    public static Date addMinute(Date src, Integer value, Date maxData) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(src);
        calendar.add(Calendar.MINUTE, value);
        src = calendar.getTime();
        if (src.after(maxData)) {
            return maxData;
        }
        return src;
    }

    public static Date addHour(Date src, Integer value, Date maxData) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(src);
        calendar.add(Calendar.HOUR, value);
        src = calendar.getTime();
        if (src.after(maxData)) {
            return maxData;
        }
        return src;
    }

    public static Date addHour(Date src, Integer value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(src);
        calendar.add(Calendar.HOUR, value);
        src = calendar.getTime();
        return src;
    }

    public static Date addDays(Date src, Integer days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(src);
        calendar.add(Calendar.DATE, days);
        src = calendar.getTime();
        return src;
    }

    public static Date addMonth(Date src, Integer value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(src);
        calendar.add(Calendar.MONTH, value);
        src = calendar.getTime();
        return src;
    }

    public static Date addYear(Date src, Integer value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(src);
        calendar.add(Calendar.YEAR, value);

        src = calendar.getTime();
        return src;
    }

    public static Date nowTime() {
        return new Date();
    }

    public static int subTimeMinute(Date src1, Date src2) {
        return (int) ((src1.getTime() - src2.getTime()) / (1000 * 60));
    }

    public static String getDateHm(Date date) {
        return formatStrScd.format(date);
    }

    public static String getDateYmD(Date date) {
        return formaterFull.format(date);
    }

    /**
     * 时间相加1小时 *
     */
    public static Date addOneHour(Date startDate, Integer hours) {
        startDate.setMinutes(startDate.getHours() + hours);
        return startDate;
    }

    //判断<firstDate>时间点是否在<secondDate>时间点之前
    public static boolean isBefore(Date firstDate, Date secondDate) {

        return compare(firstDate, secondDate) < 0 ? true : false;
    }

    public static int compare(Date firstDate, Date secondDate) {
        Calendar firstCalendar = null;
        if (firstDate != null) {
            firstCalendar = Calendar.getInstance();
            firstCalendar.setTime(firstDate);
        }
        Calendar secondCalendar = null;
        if (firstDate != null) {
            secondCalendar = Calendar.getInstance();
            secondCalendar.setTime(secondDate);
        }
        try {
            return firstCalendar.compareTo(secondCalendar);
        } catch (NullPointerException e) {
            throw new IllegalArgumentException(e);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(e);
        }
    }
}

你可能感兴趣的:(日期)