时间工具类

来源:互联网 发布:根据域名查询ip地址 编辑:程序博客网 时间:2024/06/03 17:59
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    private static final DateFormat ymdhmsFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");

    public static final DateFormat ymFormat = new SimpleDateFormat("yyyy-MM");

    public static final DateFormat ymdFormat = new SimpleDateFormat( "yyyy-MM-dd");

    private static final DateFormat mdhmFormat = new SimpleDateFormat("MM-dd HH:mm");

    private static final DateFormat hmsFormat = new SimpleDateFormat("HH:mm:ss");

    private static final DateFormat yMdHmsFormatFormat = new SimpleDateFormat( "yyyyMMddHHmmss");

    private static final long MillisecondsPerDay = 24 * 60 * 60 * 1000;

    private static final long MillisecondsPerHour = 60 * 60 * 1000;

    public static final String yMdHmsFormat(Date date) {
        if (date == null) {
            return "";
        }
        return yMdHmsFormatFormat.format(date);
    }

    /**
     * yyyyMMddHHmmss
     *
     * @param args
     * @throws ParseException
     */
    public static Date getDateByYMdHms(String str) throws ParseException {
        if (str == null || str.length() < 14) {
            return null;
        }
        return yMdHmsFormatFormat.parse(str);
    }

    /**
     *
     * @Title: currentDate
     * @Description: 当前日期(yyyy-MM-dd)
     * @param @return
     * @return Date
     */
    public static Date currentDate() {
        return timeToDate(new Date());
    }

    /**
     *
     * @Title: currentDay
     * @Description: 当前日期字符串(yyyy-MM-dd)
     * @param @return
     * @return Date
     */
    public static String currentDay() {
        return fmt2DayStr(new Date());
    }

    /**
     *
     * @Title: currentDate
     * @Description: 当前日期时间字符串(yyyy-MM-dd HH:mm:ss)
     * @param @return
     * @return Date
     */
    public static String currentTime() {
        return fmt2TimeStr(new Date());
    }

    /**
     * @Title: timeToDate
     * @Description:把日期后的时间归0 变成(yyyy-MM-dd 00:00:00:000)
     * @param date
     * @return Date
     */
    public static Date timeToDate(Date dateTime) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dateTime);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
    
    /**
     * @Title: timeToDate2
     * @Description:把日期后的时间归0 变成(yyyy-MM-dd 23:59:59:000)
     * @param date
     * @return Date
     */
    public static Date timeToDate2(Date dateTime) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dateTime);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     *
     * @Title: str2Day
     * @Description: 字符串时间转化为date(yyyy-MM-dd)
     * @param @param strDate
     * @param @return
     * @return Date
     */
    public static Date str2Day(String strDate) {
        if (strDate == null)
            return null;
        Date ret = new Date();
        try {
            ret = ymdFormat.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     *
     * @Title: toDate
     * @Description: 字符串时间转化为date(yyyy-MM-dd HH:mm:ss)
     * @param @param strDate
     * @param @return
     * @return Date
     */
    public static Date str2Time(String strDate) {
        if (strDate == null)
            return null;
        Date ret = new Date();
        try {
            ret = ymdhmsFormat.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     *
     * @Title: fmt2DayStr
     * @Description: 把日期格式转换成格式(yyyy-MM-dd)
     * @param @param date
     * @param @return
     * @return String
     */
    public static String fmt2DayStr(Date date) {
        if (date == null)
            return null;
        return ymdFormat.format(date);
    }

    /**
     *
     * @Title: fmt2TimeStr
     * @Description: 把日期格式转换成格式(yyyy-MM-dd HH:mm:ss)
     * @param @param date
     * @param @return
     * @return String
     */
    public static String fmt2TimeStr(Date date) {
        if (date == null)
            return null;
        return ymdhmsFormat.format(date);
    }

    /**
     * MM-dd HH:mm格式转换
     *
     * @param date
     * @return
     */
    public static String mdhmTime2Str(Date date) {
        if (date == null)
            return null;
        return mdhmFormat.format(date);
    }

    /**
     *
     * @Title: format
     * @Description: 格式化日期字符串
     * @param @param date 日期
     * @param @param pattern 日期格式
     * @param @return
     * @return String
     */
    public static String format(Date date, String pattern) {
        DateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 把一个表示日期的字符串转化成一个指定格式的日期类型
     *
     * @param strDate
     * @param aMask
     * @return
     * @throws ParseException
     */
    public static final Date convertStringToDate(String strDate, String aMask)
            throws ParseException {
        if (strDate == null) {
            return null;
        }
        if (aMask == null) {
            return null;
        }
        DateFormat format = new SimpleDateFormat(aMask);
        return format.parse(strDate);
    }

    /**
     * @Title: getDayDiff
     * @Description: 返回两个时间差(天)
     * @param @param d1
     * @param @param d2
     * @param @return
     * @return int
     */
    public static final int getDayDiff(Date d1, Date d2) {
        long d1t = d1.getTime();
        long d2t = d2.getTime();
        return (int) ((d2t - d1t) / MillisecondsPerDay);
    }

    /**
     * @Title: getHourDiff
     * @Description: 返回两个时间差(小时)
     * @param @param d1
     * @param @param d2
     * @param @return
     * @return int
     */
    public static double getHourDiff(Date d1, Date d2) {
        long d1t = d1.getTime();
        long d2t = d2.getTime();
        return ((d2t - d1t) / MillisecondsPerHour);
    }

    /**
     * @Title: getSecondDiff
     * @Description: 返回两个时间差(秒)
     * @param @param d1
     * @param @param d2
     * @param @return
     * @return int
     */
    public static long getSecondDiff(Date d1, Date d2) {
        long d1t = d1.getTime();
        long d2t = d2.getTime();
        return ((d2t - d1t) / 1000);
    }

    /**
     * @Title: getDayAdd
     * @Description: 计算N天后的日期时间
     * @param @param date
     * @param @param days
     * @param @return
     * @return Date
     */
    public static Date getDateAdd(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
        return new Date(cal.getTime().getTime());
    }

    /**
     * @disc 根据日期获取相差的月份
     * @param date
     *            :给定的日期
     * @param months
     *            :相差的月份数
     * @return 返回Date对象
     */
    static public Date getMonthAdd(Date date, int months) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, months);

        return new Date(cal.getTime().getTime());
    }

    /**
     *
     * @Title: getNumFmtDate
     * @Description: 获得当前时间,格式如:20140128150501
     * @param @param date
     * @param @return
     * @return String
     */
    public static String getNumFmtDate(Date date) {
        String str = fmt2TimeStr(date);
        str = StringUtil.replaceChars(str, "-", "");
        str = StringUtil.replaceChars(str, ":", "");
        str = StringUtil.replaceChars(str, " ", "");
        return str;
    }

    /**
     *
     * @Title: getNumStr2Time
     * @Description: 字符串"20140128150501" 转换为 date "2014-01-28 15:05:01"
     * @param @param str
     * @param @return
     * @param @throws ParseException
     * @return Date
     */
    public synchronized static Date getNumStr2Time(String str)
            throws ParseException {
        if (str == null || str.length() < 14) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        sb.append(str.substring(0, 4)).append("-");
        sb.append(str.substring(4, 6)).append("-");
        sb.append(str.substring(6, 8)).append(" ");
        sb.append(str.substring(8, 10)).append(":");
        sb.append(str.substring(10, 12)).append(":");
        sb.append(str.substring(12, 14));
        return str2Time(sb.toString());
    }

    /**
     * 获取月份的最后一天
     *
     * @param date
     * @return
     */
    public static Date getMonthLastDay(Date date) {
        Date nextMonthday = getFirstDateOfLastMon(date);
        return getDateAdd(nextMonthday, -1);
    }

    /**
     * 获取本月的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * @disc 根据当前日期,取得下个月第一天,2009-10-19 14:59:59
     * @param
     * @param mon
     * @return
     */
    static public Date getFirstDateOfLastMon(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.add(Calendar.MONTH, 1);

        return new Date(cal.getTime().getTime());
    }

    /**
     * 获取days天之后订单的关闭时间,如20120621000005
     *
     * @return
     */
    public static String getOutTime(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, days);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 5);
        cal.set(Calendar.MILLISECOND, 0);
        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(new Date(cal.getTime().getTime()));
    }

    /**
     * 判断两个指定格式的日期字符串前后,如果第一个日期字符串在第二个字符串前,返回-1 如果第一个日期字符串在第二个字符串后,返回1
     * 如果第一个日期字符串等于第二个字符串,返回0
     *
     */
    public static final int compare(String one, String two, String aMask)
            throws ParseException {

        return (convertStringToDate(one, aMask).compareTo(convertStringToDate(
                two, aMask)));
    }

    /**
     * 日期作比较
     *
     * @return two与one的差值(天) author:金龙
     */
    public static int compare(Date one, Date two) {
        Long num1 = one.getTime();
        Long num2 = two.getTime();
        return (int) ((num2 - num1) / (24 * 3600 * 1000));
    }

    /**
     * 日期作比较
     *
     * @return two与one的差值(天)
     */
    public static int compareDay(Date one, Date two) {
        Date oneYMDDate = DateUtil.getYMDDate(one);
        Date twoYMDDate = DateUtil.getYMDDate(two);
        return DateUtil.compare(oneYMDDate, twoYMDDate);
    }

    public static Date getYMDDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 是否在今天范围内
     *
     * @param date
     * @return
     */
    public static boolean inCurrentDate(Date date) {
        if (null == date) {
            return false;
        }
        return fmt2DayStr(date).equals(fmt2DayStr(new Date()));
    }

    public static final String ymFormat(Date date) {
        if (date == null)
            return "";
        return ymFormat.format(date);
    }

    /**
     * 加减天数
     *
     * @param date
     * @return Date
     */
    public static final Date increaseDate(Date aDate, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(aDate);
        cal.add(Calendar.DAY_OF_MONTH, days);
        return cal.getTime();
    }

    public static Date getDateAfterDay(Date date, int day) {
        if (date == null)
            return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, day);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 5);
        cal.set(Calendar.MILLISECOND, 0);
        return new Date(cal.getTime().getTime());
    }

    /**
     * 返回两时间的时间间隔(以天计算)
     *
     * @param time1
     * @param time2
     * @return
     */
    static public Long spaceDay(Date date1, Date date2) {
        Long num1 = date1.getTime();
        Long num2 = date2.getTime();
        Long space = (num2 - num1) / (1000 * 3600 * 24);
        return space;
    }

}

0 0