使用joda-time封裝的日期工具類

1、Joda-time簡介
Joda-Time這個專門處理日期時間的庫
2、類說明
Instant:不可變類,代表時間線上的一個瞬時的時間點
DateTime:不可變類,它以毫秒級的精度封裝時間上的某個瞬間時刻,用來替換JDK的Calendar類
LocalDate:不可變類,該類封裝了一個年/月/日的組合。沒有時區信息
LocalTime:不可變類,表示一個本地的時間,而不包含日期部分。沒有時區信息
LocalDateTime:不可變類,該類封裝了一個年/月/日 時:分:秒的組合。沒有時區信息
詳細代碼如下:
封裝了常見的日期處理方式

public class DateUtils {


    public static  final String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";

    public static final String FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static final String FORMAT_CHINESE = "yyyy年M月d日";

    public static final String FORMAT_HH_MM = "HH:mm";

    public static final String FORMAT_YMDHMS = "yyyyMMdd hh:mm:ss";

    public static final String START_TIME = "00:00:00";

    public static final String END_TIME = "23:59:59";

    public static DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(FORMAT_YYYY_MM_DD_HH_MM_SS);


    /**
     * 獲取系統當前時間 返回 yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getCurrentTime() {
        DateTime dt = new DateTime();
        String time = dt.toString(FORMAT_YYYY_MM_DD_HH_MM_SS);
        return time;
    }

    /**
     * 獲取系統當前時間按照指定格式返回
     * @param pattern
     * @return
     */
    public static String getCurrentTimePattern(String pattern) {
        DateTime dt = new DateTime();
        String time = dt.toString(pattern);
        return time;
    }

    /**
     * 獲取當前日期
     * @return yyyy-MM-dd
     */
    public static String getCurrentDate() {
        DateTime dt = new DateTime();
        String date = dt.toString(FORMAT_YYYY_MM_DD);
        return date;
    }

    /**
     * 獲取當前日期按照指定格式
     * @param pattern
     * @return
     */
    public static String getCurrentDatePattern(String pattern) {
        DateTime dt = new DateTime();
        String date = dt.toString(pattern);
        return date;
    }

    /**
     * 按照時區轉換時間
     * @param date
     * @param timeZone 時區
     * @param pattern
     * @return
     */
    public static String format(Date date, TimeZone timeZone, String pattern) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        sdf.setTimeZone(timeZone);
        return sdf.format(date);
    }

    /**
     * 獲取指定時間
     * @param year 年
     * @param month 月
     * @param day 天
     * @param hour 小時
     * @param minute 分鐘
     * @param seconds 秒
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getPointTime(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer seconds) {
        DateTime dt = new DateTime(year, month, day, hour, minute, seconds);
        String date = dt.toString(FORMAT_YYYY_MM_DD_HH_MM_SS);
        return date;
    }

    /**
     * 獲取指定時間 指定返回格式
     * @param year 年
     * @param month 月
     * @param day 天
     * @param hour 小時
     * @param minute 分鐘
     * @param seconds 秒
     * @param pattern 自定義格式
     * @return pattern
     */
    public static String getPointTimePattern(Integer year, Integer month, Integer day, Integer hour,
                                             Integer minute, Integer seconds, String pattern) {
        DateTime dt = new DateTime(year, month, day, hour, minute, seconds);
        String date = dt.toString(pattern);
        return date;
    }

    /**
     * 獲取指定日期
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static String getPointDate(Integer year, Integer month, Integer day) {
        LocalDate dt = new LocalDate(year, month, day);
        String date = dt.toString(FORMAT_YYYY_MM_DD);
        return date;
    }

    /**
     * 獲取指定日期 返回指定格式
     * @param year
     * @param month
     * @param day
     * @param pattern
     * @return
     */
    public static String getPointDatPattern(Integer year, Integer month, Integer day, String pattern) {
        LocalDate dt = new LocalDate(year, month, day);
        String date = dt.toString(pattern);
        return date;
    }


    /**
     * 獲取當前是一週星期幾
     * @return
     */
    public static String getWeek() {
        DateTime dts = new DateTime();
        return DayWeekEnum.getNameByCode(dts.getDayOfWeek());
    }


    /**
     * 獲取指定時間是一週的星期幾
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static String getWeekPoint(Integer year, Integer month, Integer day) {
        LocalDate dts = new LocalDate(year, month, day);
        return DayWeekEnum.getNameByCode(dts.getDayOfWeek());
    }


    /**
     * 格式化日期 日期轉爲字符串
     * @param date
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String format(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_YYYY_MM_DD_HH_MM_SS);
        return format.format(date);
    }

    /**
     * 日期轉爲字符串 指定格式
     * @param date 日期
     * @param pattern 日期格式
     * @return
     */

    public static String format(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 字符串轉爲日期 指定格式
     * @param date 日期字符串
     * @param pattern 日期格式
     * @return
     */
    public static Date parse(String date, String pattern) {
        if (date == null) {
            return null;
        }
        Date resultDate = null;
        try {
            resultDate = new SimpleDateFormat(pattern).parse(date);
        } catch (ParseException e) {
            throw new GenericBusinessException(e);
        }
        return resultDate;
    }

    /**
     * 字符串轉爲日期
     * @param date 日期字符串
     * @return
     */
    public static Date parse(String date) {
        if (date == null) {
            return null;
        }
        try {
            return new SimpleDateFormat(FORMAT_YYYY_MM_DD_HH_MM_SS).parse(date);
        } catch (ParseException e) {
            throw new GenericBusinessException(e);
        }
    }

    /**
     * 毫秒數轉爲字符串 按照指定格式轉換
     * @param timestamp
     * @return
     */
    public static String format(Long timestamp, String pattern) {
        String dateStr = "";
        if (null == timestamp || timestamp.longValue() < 0) {
            return dateStr;
        }
        try {
            Date date = new Date(timestamp);
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            dateStr = format.format(date);
        } catch (Exception e) {
            throw new GenericBusinessException(e);
        }
        return dateStr;
    }

    /**
     *獲取當前時間前幾天時間,按指定格式返回
     * @param days
     * @return
     */
    public static String forwardDay(Integer days, String format) {
        DateTime dt = new DateTime();
        DateTime y = dt.minusDays(days);
        return y.toString(format);
    }

    /**
     * 獲取當前時間前幾天時間
     * @param days
     * @return
     */
    public static Date forwardDay(Integer days) {
        DateTime dt = new DateTime();
        DateTime y = dt.minusDays(days);
        return y.toDate();
    }

    /**
     * 計算兩個時間相差多少天
     * @param startDate
     * @param endDate
     * @return
     */
    public static Integer diffDay(Date startDate, Date endDate) {
        if (startDate == null || endDate == null) {
            return null;
        }
        DateTime dt1 = new DateTime(startDate);
        DateTime dt2 = new DateTime(endDate);
        int day = Days.daysBetween(dt1, dt2).getDays();
        return Math.abs(day);
    }

    /**
     * 獲取指定間隔天數的日期
     * @param date
     * @param offset
     * @return
     */
    public static Date addDay(Date date, int offset) {
        DateTime dt1;
        if (date == null) {
            dt1 = new DateTime().plusDays(offset);
            return dt1.toDate();
        }
        dt1 = new DateTime(date).plusDays(offset);
        return dt1.toDate();
    }

    /**
     * 獲取日期的開始時間
     * @param dateStr
     * @return
     */
    public static Date getDateStart(String dateStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT_YMDHMS);
        String subStr = dateStr.substring(0,8);
        try {
            Date date = simpleDateFormat.parse(subStr + " " + START_TIME);
            return date;
        } catch (ParseException e) {
            throw new GenericBusinessException(e);
        }
    }

    /**
     * 獲取日期的結束時間
     * @param dateStr
     * @return
     */
    public static Date getDateEnd(String dateStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT_YMDHMS);
        String subStr = dateStr.substring(0,8);
        try {
            Date date = simpleDateFormat.parse(subStr + " " + END_TIME);
            return date;
        } catch (ParseException e) {
            throw new GenericBusinessException(e);
        }
    }

辛苦點贊
源碼地址

發佈了94 篇原創文章 · 獲贊 106 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章