时间处理工具类

转载 2013年12月03日 17:46:29
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
*
* 日期操作的工具类
*
* @author huwei(jshuwei.org.cn)
* @since 1.0
*
*/
public class DateUtil {

    private static String defDtPtn = "yyyy-MM-dd HH:mm:ss";// 缺省日期格式

    /**
     * 计算给定时间至今的天数
     *
     * @since 1.1
     * @param date
     *            给定的时间
     * @return 给定时间至今的天数
     */
    public static long date2day(String date) {
        long day = 0;
        DateFormat df = DateFormat.getDateInstance();
        try {
            long old = df.parse(date).getTime();
            long now = new java.util.Date().getTime();
            long secs = now - old;
            day = secs / (1000 * 24 * 60 * 60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day;
    }

    /**
     * 格式化给定时间后day天的时间
     *
     * @since 1.0
     * @param date
     *            需要格式化的时间
     * @param day
     *            增加的天数
     * @return 给定时间的后day天的格式化字符串(如:2008-11-22)
     */
    public static String formatDate(java.util.Date date, Integer day) {
        String str = "";
        str = new Date(date.getTime() day * 24 * 60 * 60).toString();
        return str;
    }

    /**
     * 格式化给定时间
     *
     * @param date
     *            需要格式化的时间
     * @return 给定时间的格式化字符串(如:2008-11-22)
     */
    public static String formatDate(java.util.Date date) {
        return new Date(date.getTime()).toString();
    }

    /**
     * 得到当前年
     *
     * @since 1.0
     * @return 返回当前年(YYYY)
     */
    public static int getYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    /**
     * 得到当前月
     *
     * @since 1.0
     * @return 返回当前月(1~12)
     */
    public static int getMonth() {
        return Calendar.getInstance().get(Calendar.MONTH) 1;
    }

    /**
     * 得到当前日
     *
     * @since 1.0
     * @return 返回当前日(1~31)
     */
    public static int getDay() {
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 得到当前年
     *
     * @since 1.0
     * @return 返回当前年(YY)
     */
    public static String getYear2() {
        int year = getYear();
        return StringUtil.Integer2String(year, "1986").substring(2, 4);
    }

    /**
     * 得到当前月
     *
     * @since 1.0
     * @return 返回当前月(01~12)
     */
    public static String getMonth2() {
        int month = getMonth();
        if (month < 10) {
            return "0" StringUtil.Integer2String(month, "00");
        }
        return StringUtil.Integer2String(month, "00");
    }

    /**
     * 得到当前日
     *
     * @since 1.0
     * @return 返回当前日(01~31)
     */
    public static String getDay2() {
        int day = getDay();
        if (day < 10) {
            return "0" StringUtil.Integer2String(day, "00");
        }
        return StringUtil.Integer2String(day, "00");
    }

    /**
     * 得到指定格式的当前时间
     *
     * @param format
     *            格式化形式(年用YY/YYYY表示;月用M/MM表示;日用D/DD表示,之间任意任序组合),<br />
     *            如"YYYY-MM-DD"返回形如:1986-12-17<br />
     *            如"YY-MM"返回形如:86-12<br />
     *            如"YY年MM月"返回形如:86年12月……
     * @since 1.0
     * @return 返回指定格式的当前时间
     *
     */
    public static String getDate(String format) {
        format = format.replace("YYYY", getYear() "");
        format = format.replace("YY", getYear2());
        format = format.replace("MM", getMonth2());
        format = format.replace("M", getMonth() "");
        format = format.replace("DD", getDay2());
        format = format.replace("D", getDay() "");
        return format;
    }

    /**
     * 将字符串按指定格式解析成日期对象
     *
     * @since 1.1
     * @param dateStr
     *            需要进行转换的日期字符串
     * @param pattern
     *            日期字符串的格式
     * @return "yyyy-MM-dd HH:mm:ss"形式的日期对象
     */
    public static java.util.Date parseDate(String dateStr, String pattern) {
        SimpleDateFormat DATEFORMAT = new SimpleDateFormat(defDtPtn);
        DATEFORMAT.applyPattern(pattern);
        java.util.Date ret = null;
        try {
            ret = DATEFORMAT.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        DATEFORMAT.applyPattern(defDtPtn);
        return ret;
    }

    /**
     * 计算详细年龄
     *
     * @since 1.1
     * @param birthdayStr
     *            出生日期 格式"YYYY-MM-DD"
     * @return 指定日期至今天的年龄
     */
    public static String countAge(String birthdayStr) {
        String age = "";
        if (birthdayStr != null && birthdayStr.length() == 8) {
            java.util.Date birthday = parseDate(birthdayStr, "YYYY-MM-DD");

            if (birthday != null) {
                Calendar calendar = Calendar.getInstance();
                int year1 = getYear();
                int month1 = StringUtil.String2Integer(getMonth2(), 0);
                int day1 = StringUtil.String2Integer(getDay2(), 00);

                calendar.setTime(birthday);
                int year2 = calendar.get(Calendar.YEAR);
                int month2 = calendar.get(Calendar.MONTH) 1;
                int day2 = calendar.get(Calendar.DATE);

                int year = year1 - year2;
                int month = month1 - month2;
                int day = day1 - day2;

                if (month < 0) {
                    year = year - 1;
                    month = 12 month1 - month2;
                }

                if (day < 0) {
                    month = month - 1;
                    if (month < 0) {
                        year = year - 1;
                        month = 11;
                    }
                    int lastMonthDay = 0;
                    if (month1 == 0) {
                        lastMonthDay = getDayOfMonth(12, year1 - 1);
                    } else {
                        lastMonthDay = getDayOfMonth(month1, year1);
                    }
                    day = lastMonthDay day1 - day2;
                }

                if (year > 5) {
                    age = year "岁";
                } else if (year > 0) {
                    if (month == 0) {
                        age = year "岁";
                    } else {
                        age = year "岁" month "月";
                    }
                } else {
                    if (month > 5) {
                        age = month "月";
                    } else if (month > 0) {
                        age = month "月" day "天";
                    } else {
                        age = day "天";
                    }
                }
            }
        }

        return age;
    }

    /**
     * 得到指定年月的天数
     *
     * @since 1.1
     * @param month
     *            指定月份
     * @param year
     *            指定的年份
     * @return 天数
     */
    public static int getDayOfMonth(int month, int year) {
        int ret = 0;
        boolean flag = false;

        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            flag = true;
        }

        switch (month) {
        case 1:
            ret = 31;
            break;
        case 2:
            if (flag) {
                ret = 29;
            } else {
                ret = 28;
            }
            break;
        case 3:
            ret = 31;
            break;
        case 4:
            ret = 30;
            break;
        case 5:
            ret = 31;
            break;
        case 6:
            ret = 30;
            break;
        case 7:
            ret = 31;
            break;
        case 8:
            ret = 31;
            break;
        case 9:
            ret = 30;
            break;
        case 10:
            ret = 31;
            break;
        case 11:
            ret = 30;
            break;
        case 12:
            ret = 31;
            break;
        default:
            break;
        }
        return ret;
    }

    /**
     * 计算某天是星期几
     *
     * @since 1.1
     * @param p_date
     *            日期字符串
     * @return 星期
     */
    public static int whatDayIsSpecifyDate(String p_date) {
        // 2002-2-22 is friday5
        long differenceDays = 0L;
        long m = 0L;
        differenceDays = signDaysBetweenTowDate(p_date, "2002-2-22");

        m = (differenceDays % 7);
        m = m 5;
        m = m > 7 ? m - 7 : m;
        return Integer.parseInt(m "");
    }

    /**
     * 计算两日期间相差天数.
     *
     * @since 1.1
     * @param d1
     *            日期字符串
     * @param d2
     *            日期字符串
     * @return long 天数
     */
    public static long signDaysBetweenTowDate(String d1, String d2) {
        java.sql.Date dd1 = null;
        java.sql.Date dd2 = null;
        long result = -1l;
        try {
            dd1 = java.sql.Date.valueOf(d1);
            dd2 = java.sql.Date.valueOf(d2);
            result = signDaysBetweenTowDate(dd1, dd2);
        } catch (Exception ex) {
            result = -1;
        }
        return result;
    }

    /**
     * 计算两日期间相差天数.
     *
     * @since 1.1
     * @param d1
     *            开始日期 日期型
     * @param d2
     *            结束日期 日期型
     * @return long 天数
     */
    public static long signDaysBetweenTowDate(java.sql.Date d1, java.sql.Date d2) {
        return (d1.getTime() - d2.getTime()) / (3600 * 24 * 1000);
    }
}

举报

相关文章推荐

超级全的时间处理工具类(格式化,精确,以及自己定制)

详细请看注释

TimeUtils,有关时间处理的工具类

介绍一些关于时间处理的方法,在日常开发中时常用到,已经封装好,末尾有源码下载。

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

时间处理工具类

/** * @filename DateUtils.java * @version www.mindai.com * @date 2016年5月31日 */ p...

Java开发时间处理工具类(全)

java开发过程中常用的一些事件处理工具类

java日期时间处理工具类

import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; ...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)