Java日期通用工具

来源:互联网 发布:linux oracle 编辑:程序博客网 时间:2024/05/17 02:15
/** * 日期通用工具 * @author zhaohuihua */public class DateTools {    /** 工具类, 不可实例化 **/    private DateTools() {}    private static final Format DATE_FORMAT = new Format("yyyy-MM-dd");    private static final Format DATE_TIME_FORMAT =         new Format("yyyy-MM-dd HH:mm:ss");    private static final Format DATE_LONG_FORMAT =         new Format("yyyy-MM-dd HH:mm:ss.SSS");    private static final Format TIME_LONG_FORMAT = new Format("HH:mm:ss.SSS");    private static final Format TIME_FORMAT = new Format("HH:mm:ss");    /** 将时区设置为格林威治标准时间时区, 避免将0:00解释为默认时区GMT+8的8:00 **/    private static final Format INTERVAL_FORMAT =         new Format("HH:mm:ss.SSS", "GMT+0");    /** 将日期转化为字符串(yyyy-MM-dd HH:mm:ss.SSS) **/    public static String dateToString(Date date) {        return DATE_LONG_FORMAT.getFormat().format(date);    }    /** 将日期转化为字符串(yyyy-MM-dd HH:mm:ss.SSS) **/    public static String dateToString(long date) {        return DATE_LONG_FORMAT.getFormat().format(new Date(date));    }    /** 将日期转化为字符串(yyyy-MM-dd HH:mm:ss) **/    public static String dateToNormal(Date date) {        return DATE_TIME_FORMAT.getFormat().format(date);    }    /** 将日期转化为字符串(yyyy-MM-dd HH:mm:ss) **/    public static String dateToNormal(long date) {        return DATE_TIME_FORMAT.getFormat().format(new Date(date));    }    /** 将时间转化为字符串(HH:mm:ss.SSS) **/    public static String timeToString(Date time) {        return TIME_LONG_FORMAT.getFormat().format(time);    }    /** 将时间转化为字符串(HH:mm:ss.SSS) **/    public static String timeToString(long time) {        return TIME_LONG_FORMAT.getFormat().format(new Date(time));    }    /** 将时间转化为字符串(HH:mm:ss) **/    public static String timeToNormal(Date time) {        return TIME_FORMAT.getFormat().format(time);    }    /** 将时间转化为字符串(HH:mm:ss) **/    public static String timeToNormal(long time) {        return TIME_FORMAT.getFormat().format(new Date(time));    }    /**     * 解析日期字符串     * @param date String 日期字符串     * @return Date 日期对象     * @throws IllegalArgumentException 日期格式错误     */    public static Date dateValueOf(String date) throws IllegalArgumentException {        if(Validator.isNull(date)) {            throw new IllegalArgumentException("日期格式错误:[null]");        }        try {            return _parseDate(date);        } catch(ParseException e) {            throw new IllegalArgumentException("日期格式错误:[" + date + "]");        }    }    /**     * 解析日期字符串, 解析失败返回null<br>     * @param date String 日期字符串     * @return Date 日期对象, 解析失败返回null     */    public static Date parseDate(String date) {        if(Validator.isNull(date)) return null;        try {            return _parseDate(date);        } catch(ParseException e) {            return null;        }    }    private static Date _parseDate(String date) throws ParseException {        if(date.indexOf('.') != -1)            return DATE_LONG_FORMAT.getFormat().parse(date);        else if(date.indexOf(':') != -1)            return DATE_TIME_FORMAT.getFormat().parse(date);        else            return DATE_FORMAT.getFormat().parse(date);    }    /**     * 解析时间字符串     * @param time String 时间字符串     * @return Date 日期对象     * @throws IllegalArgumentException 时间格式错误     */    public static Date timeValueOf(String time) throws IllegalArgumentException {        try {            return _parseTime(time);        } catch(ParseException e) {            throw new IllegalArgumentException("时间格式错误:[" + time + "]");        }    }    /**     * 解析时间字符串, 解析失败返回null<br>     * @param time String 时间字符串     * @return Date 日期对象     */    public static Date parseTime(String time) {        try {            return _parseTime(time);        } catch(ParseException e) {            return null;        }    }    private static Date _parseTime(String time) throws ParseException {        if(time.indexOf('.') != -1)            return TIME_LONG_FORMAT.getFormat().parse(time);        else            return TIME_FORMAT.getFormat().parse(time);    }    /**     * 格式化间隔时间(dDays HH:mm:ss.SSS)     * @param interval long 间隔时间     * @return String     */    public static String formatInterval(long interval) {        // 时分秒毫秒        String result = INTERVAL_FORMAT.getFormat().format(new Date(interval));        // 天        long day = interval / 86400000;        if(day > 0) { // 大于1天, dDays HH:mm:ss.SSS            result = day + (day == 1 ? "Day " : "Days ") + result;        } else { // 小于1天, HH:mm:ss.SSS            return result;        }    }    /**     * 获取以字符串形式表示的间隔时间(dDays HH:mm:ss.SSS)     * @param start long     * @param end long     * @return String     */    public static String getInterval(long start, long end) {        if(start > end) {            long temp = start;            start = end;            end = temp;        }        return formatInterval(end - start);    }    /**     * 获取以字符串形式表示的间隔时间(dDays HH:mm:ss.SSS)     * 指定时间与当前时间的间隔     * @param start long     * @return String     */    public static String getInterval(long start) {        return getInterval(start, System.currentTimeMillis());    }    /**     * 获取以字符串形式表示的间隔时间(dDays HH:mm:ss.SSS)     * @param start Date     * @param end Date     * @return String     */    public static String getInterval(Date start, Date end) {        return getInterval(start.getTime(), end.getTime());    }    /**     * 获取以字符串形式表示的间隔时间(dDays HH:mm:ss.SSS)     * 指定时间与当前时间的间隔     * @param start Date     * @return String     */    public static String getInterval(Date start) {        return getInterval(start.getTime(), System.currentTimeMillis());    }    /**     * 判断日期是否已经过期     * @param date     * @return     */    public static boolean isOverdue(String date) {        try {            return dateValueOf(date).getTime() < System.currentTimeMillis();        } catch(Exception e) {            return false;        }    }    /**     * 判断当前时间是否在开始时间与结束时间之间<br>     * 以大于等于开始时间且小于结束时间作判断<br>     * 如果结束时间没有时间部分, 则默认为包括当天的全部时间段     * @param start 开始时间     * @param end 结束时间     * @return     */    public static boolean in(String start, String end) {        return in(start, end, (Date)null);    }    /**     * 判断指定时间是否在开始时间与结束时间之间<br>     * 以大于等于开始时间且小于结束时间作判断<br>     * 如果结束时间没有时间部分, 则默认为包括当天的全部时间段     * @param start 开始时间     * @param end 结束时间     * @param dest 指定时间     * @return     */    public static boolean in(String start, String end, String dest) {        Date destDate = dest == null ? new Date() : dateValueOf(dest);        return in(start, end, destDate);    }    /**     * 判断指定时间是否在开始时间与结束时间之间<br>     * 以大于等于开始时间且小于结束时间作判断<br>     * 如果结束时间没有时间部分, 则默认为包括当天的全部时间段     * @param start 开始时间     * @param end 结束时间     * @param dest 指定时间     * @return     */    public static boolean in(String start, String end, Date dest) {        Date startDate = dateValueOf(start);        Date endDate = dateValueOf(end);        if(dest == null) dest = new Date();        long startTime = startDate.getTime();        long endTime = endDate.getTime();        long destTime = dest.getTime();        // 如果结束时间没有时间部分, 则默认为包括当天的全部时间段        // 以小于下一天的最小时间计算        if(end.indexOf(':') == -1) endTime += 24 * 60 * 60 * 1000;        return destTime >= startTime && destTime < endTime;    }    /** 本地时间与UTC的差值, UTC(通用协调时, Universal Time Coordinated) **/    private static final long UTC = TimeZone.getDefault().getOffset(0);    /** 一天的毫秒数 **/    private static final long DAY = 24 * 60 * 60 * 1000L;    /** 指定时间是否为同一天 **/    public static boolean isSameDay(long one, long two) {        // 时间毫秒数+UTC差值=本地时间        return (one + UTC) / DAY == (two + UTC) / DAY;    }    /** 指定时间是否为同一天 **/    public static boolean isSameDay(Date one, Date two) {        return isSameDay(one.getTime(), two.getTime());    }    /** 指定时间是否为当天 **/    public static boolean isToday(long date) {        return isSameDay(date, System.currentTimeMillis());    }    /** 指定时间是否为当天 **/    public static boolean isToday(Date date) {        return isSameDay(date.getTime(), System.currentTimeMillis());    }    /** 解决SimpleDateFormat线程不安全的问题 **/    private static class Format extends ThreadLocal {        private String pattern;        private String timeZone;        public Format(String pattern) {            this.pattern = pattern;        }        public Format(String pattern, String timeZone) {            this.pattern = pattern;            this.timeZone = timeZone;        }        protected synchronized Object initialValue() {            SimpleDateFormat format = new SimpleDateFormat(pattern);            if(timeZone != null) {                format.setTimeZone(TimeZone.getTimeZone(timeZone));            }            return format;        }        public SimpleDateFormat getFormat() {            return (SimpleDateFormat)get();        }    }}

0 0