分享一個關於Java日期時間的工具類

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 日期的工具類
 * 
 * @author xiaojinzi
 * 
 */
public class DateUtil {

    /**
     * 中國格式的日期:yyyy-MM-dd
     */
    public static final String CHINADATE_STYLE_1 = "yyyy-MM-dd";

    /**
     * 中國格式的日期:yyyy年MM月dd
     */
    public static final String CHINADATE_STYLE_2 = "yyyy年MM月dd";

    /**
     * 中國格式的時間:HH:mm:ss
     */
    public static final String CHINATIME_STYLE1 = "HH:mm:ss";

    /**
     * 中國格式的時間:HH時mm分ss秒
     */
    public static final String CHINATIME_STYLE2 = "HH時mm分ss秒";

    /**
     * 中國格式的日期時間:yyyy-MM-dd HH:mm:ss
     */
    public static final String CHINADATETIME_STYLE1 = "yyyy-MM-dd HH:mm:ss";

    /**
     * 中國格式的日期時間:yyyy年MM月dd HH時mm分ss秒
     */
    public static final String CHINADATETIME_STYLE2 = "yyyy年MM月dd HH時mm分ss秒";

    // 格式化對象
    private SimpleDateFormat sdf = null;

    /**
     * 構造函數
     * 
     * @param pattern
     *            匹配的字符串,這裏最好採用類中定義好的常量<br>
     *            {@link DateUtil#CHINADATE_STYLE_1} or
     *            {@link DateUtil#CHINADATE_STYLE_2} or
     *            {@link DateUtil#CHINATIME_STYLE1} or
     *            {@link DateUtil#CHINATIME_STYLE2} or
     *            {@link DateUtil#CHINADATETIME_STYLE1} or
     *            {@link DateUtil#CHINADATETIME_STYLE2}
     */
    public DateUtil(String pattern) {
        sdf = new SimpleDateFormat(pattern, Locale.CHINA);
    }

    /**
     * 返回格式化後的字符串
     * 
     * @param d
     * @return
     */
    public String formatDate(Date d) {
        return sdf.format(d);
    }

    /**
     * 返回解析後的日起對象
     * 
     * @param content
     *            要解析的字符串
     * @return
     * @throws ParseException
     *             解析失敗拋出的異常
     */
    public Date parse(String content) throws ParseException {
        return sdf.parse(content);
    }

    /**
     * 判斷某個日期是星期幾,返回1-7,分別表示星期一到星期天
     * 
     * @param date
     * @return
     */
    public static int getWeek(Date date) {
        // 創建一個把日期格式化成星期幾的格式化日期對象
        SimpleDateFormat s = new SimpleDateFormat("E", Locale.CHINA);
        String week = s.format(date);
        if (week.equals("星期一")) {
            return 1;
        } else if (week.equals("星期二")) {
            return 2;
        } else if (week.equals("星期三")) {
            return 3;
        } else if (week.equals("星期四")) {
            return 4;
        } else if (week.equals("星期五")) {
            return 5;
        } else if (week.equals("星期六")) {
            return 6;
        } else {
            return 7;
        }
    }

    /**
     * 判斷某年某月有幾天
     * 
     * @param year
     * @param month
     * @return
     */
    public static int getDayNumberOfMonth(int year, int month) {
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            return 31;
        } else if (month == 4 || month == 6 || month == 9 || month == 11) {
            return 30;
        } else {
            if (year % 4 == 0) {
                return 29;
            } else {
                return 28;
            }
        }
    }

    /**
     * 返回日期對象中的年份,整數表示
     * 
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        // 創建一個把日期格式化成月份的格式化日期對象
        SimpleDateFormat s = new SimpleDateFormat("yyyy", Locale.CHINA);
        return Integer.parseInt(s.format(date));
    }

    /**
     * 返回日期對象中的月份,整數表示1-12
     * 
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        // 創建一個把日期格式化成月份的格式化日期對象
        SimpleDateFormat s = new SimpleDateFormat("MM", Locale.CHINA);
        return Integer.parseInt(s.format(date));
    }

    /**
     * 返回日期對象中的幾號
     * 
     * @param date
     * @return
     */
    public static int getDay(Date date) {
        // 創建一個把日期格式化成日子的格式化日期對象
        SimpleDateFormat s = new SimpleDateFormat("dd", Locale.CHINA);
        return Integer.parseInt(s.format(date));
    }

    /**
     * 計算兩個時間之間隔了多少天<br>
     * 經過驗證,是準確的
     * 
     * @param startDate
     * @param endDate
     * @return
     */
    public static long getDaysBetweenDates(Date startDate, Date endDate) {
        return getDaysBetweenDates(startDate.getTime(), endDate.getTime());
    }

    /**
     * 計算兩個時間之間隔了多少天<br>
     * 經過驗證,是準確的
     * 
     * @param startDate
     * @param endDate
     * @return
     */
    public static long getDaysBetweenDates(long startDate, long endDate) {
        // 獲取到兩個時間的相差毫秒值
        long tmp = endDate - startDate;
        System.out.println("tmp=" + tmp);
        return tmp / (1000 * 60 * 60 * 24);
    }

    /**
     * 獲取兩個時間之間的小時數
     * 
     * @param startDate
     * @param endDate
     * @return
     */
    public static long getHoursBetweenDates(Date startDate, Date endDate) {
        // 獲取到兩個時間的相差毫秒值
        long tmp = endDate.getTime() - startDate.getTime();
        System.out.println("tmp=" + tmp);
        return tmp / (1000 * 60 * 60);
    }

}

//======================================================================================
以上代碼是一個平常用到的關於時間日期的工具類,裏面也許只有我平時用到的,所以肯定還有很多的不足,添加你自己的代碼進去,自己也封裝一個更完整的工具類吧!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章