日期格式化工具

日期格式化工具:java

1.日期轉換字符串
2.字符串轉換爲日期
3.獲取一天開始時間
4.獲取一天結束時間
5.獲取一週開始時間 00:00:00
6.獲取一週結束時間 23:23:59
7.獲取一個月開始時間
8.獲取一個月結束時間
9.根據年月輸出每個月最後一天
10.獲取年份
11.讀取月份
12.轉換中文日期格式git

 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

/**
 * 日期格式化工具
 *
 * @author rico 2016年5月7日
 */
public class DateFormatUtils {

    // 時間格式化格式
    public static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String DATETIME_MINUTE_PATTERN = "yyyy-MM-dd HH:mm";
    public static final String DATE_PATTERN = "yyyy-MM-dd";
    public static final String DATE_MONTH_PATTERN = "yyyy-MM";
    public static final String DATE_YEAR_PATTERN = "yyyy";
    public static final String DATE_PATTERN_CN = "yyyy年MM月dd日";

    // 周天數
    private static final int DAYS_OF_WEEK = 7;

    /**
     * 日期轉換字符串
     *
     * @param date    日期
     * @param pattern 時間格式
     * @return
     */
    public static String toString(Date date, String pattern) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        }

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 字符串轉換爲日期
     *
     * @param date    字符串格式日期
     * @param pattern 轉換類型
     * @return
     */
    public static Date toDate(String date, String pattern) throws ParseException {
        if (StringUtils.isBlank(date)) {
            throw new IllegalArgumentException("The date must not be null");
        }

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.parse(date);
    }

    /**
     * 獲取一天開始時間
     *
     * @param date
     * @return
     */
    public static Date getFirstOfDateTime(Date date) {
        return DateFormatUtils.convertDateToStartOrEnd(date, true);
    }

    /**
     * 獲取一天結束時間
     *
     * @param date
     * @return
     */
    public static Date getLastOfDateTime(Date date) {
        return DateFormatUtils.convertDateToStartOrEnd(date, false);
    }

    /**
     * 時間格式化,獲取一天開始、結束時間
     *
     * @param date
     * @param isBegin
     * @return
     */
    private static Date convertDateToStartOrEnd(Date date, boolean isBegin) {
        return convertDateToStartOrEnd(null, date, isBegin);
    }

    /**
     * 時間格式化,獲取一天開始、結束時間
     *
     * @param date    格式化時間
     * @param isBegin true:格式爲開始時間,false:格式化爲結束時間
     * @return
     */
    private static Date convertDateToStartOrEnd(Calendar cal, Date date, boolean isBegin) {
        if (cal == null) {
            cal = Calendar.getInstance();
        }
        cal.setTime(date);

        if (isBegin) {
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
        } else {
            cal.set(Calendar.HOUR_OF_DAY, 23);
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 999);
        }

        return cal.getTime();
    }

    /**
     * 獲取一週開始時間 00:00:00
     *
     * @param date
     * @return
     */
    public static Date getFirstDateTimeOfWeek(Date date) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        }

        Calendar cal = Calendar.getInstance();
        convertDateToStartOrEnd(cal, date, true);

        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DATE, 1 - dayOfWeek);

        return cal.getTime();
    }

    /**
     * 獲取一週結束時間 23:23:59
     *
     * @param date
     * @return
     */
    public static Date getLastDateTimeOfWeek(Date date) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        }

        Calendar cal = Calendar.getInstance();
        convertDateToStartOrEnd(cal, date, false);

        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DATE, DAYS_OF_WEEK - dayOfWeek);

        return cal.getTime();
    }

    /**
     * 獲取一個月開始時間
     *
     * @param date
     * @return
     */
    public static Date getFirstDateTimeOfMonth(Date date) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        }

        Calendar cal = Calendar.getInstance();
        convertDateToStartOrEnd(cal, date, true);
        cal.set(Calendar.DATE, 1);

        return cal.getTime();
    }

    /**
     * 獲取一個月結束時間
     *
     * @param date
     * @return
     */
    public static Date getLastDateTimeOfMonth(Date date) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        }

        Calendar cal = Calendar.getInstance();
        convertDateToStartOrEnd(cal, date, false);

        cal.set(Calendar.DATE, 1);
        cal.add(Calendar.MONTH, 1);
        cal.add(Calendar.DATE, -1);

        return cal.getTime();
    }

    /**
     * 根據年月輸出每個月最後一天.
     *
     * @param year  the year
     * @param month the month
     * @return the CN last date time of month
     */
    public static String getLastDateTimeOfMonth(int year, int month, String pattern) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        Date date = cal.getTime();
        return toString(getLastDateTimeOfMonth(date), pattern);
    }

    /**
     * 獲取年份
     *
     * @return
     */
    public static int getCurrentYear() {
        Calendar cal = Calendar.getInstance();
        return getYearOfDate(cal);
    }

    /**
     * 獲取年份
     *
     * @param date
     * @return
     */
    public static int getYearOfDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return getYearOfDate(cal);
    }

    /**
     * 獲取年份
     *
     * @param cal
     * @return
     */
    public static int getYearOfDate(Calendar cal) {
        return cal.get(Calendar.YEAR);
    }

    /**
     * 讀取月份
     * @param date
     * @return
     */
    public static int getMonthOfDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return getMonthOfDate(cal);
    }

    /**
     *
     * @param date
     * @return
     */
    public static int getMonthOfDate2(Date date) {
        return 1 + getMonthOfDate(date);
    }

    /**
     * 讀取月份
     * @param cal
     * @return
     */
    public static int getMonthOfDate(Calendar cal) {
        return cal.get(Calendar.MONTH);
    }

    /**
     * 轉換中文日期格式
     *
     * @param date
     * @return
     */
    public static String converToCnDate(Date date) {
        Map<String, String> separators = defaultDateSeparators();
        return converToCnDate(date, separators);
    }

    /**
     * 轉換中文日期格式
     *
     * @param date
     * @param separators
     * @return
     */
    public static String converToCnDate(Date date, Map<String, String> separators) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        return converToCnDate(cal, separators);
    }

    /**
     * 轉換中文日期格式
     *
     * @param cal
     * @return
     */
    public static String converToCnDate(Calendar cal) {
        Map<String, String> separators = defaultDateSeparators();
        return converToCnDate(cal, separators);
    }

    /**
     * 轉換中文日期格式
     *
     * @param cal
     * @param separators
     * @return
     */
    public static String converToCnDate(Calendar cal, Map<String, String> separators) {
        // 獲取年月日
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int date = cal.get(Calendar.DATE);

        String yearStr = DigitalUtil.converYearToString(year, DigitalUtil.digitToStringMap2());
        String monthStr = DigitalUtil.converDigitToString(month);
        String dateStr = DigitalUtil.converDigitToString(date);

        return yearStr.concat(separators.get("year"))
                .concat(monthStr).concat(separators.get("month"))
                .concat(dateStr).concat(separators.get("date"));
    }

    /**
     * 默認日期格式化格式
     *
     * @return
     */
    public static Map<String, String> defaultDateSeparators() {
        Map<String, String> separators = new HashMap<String, String>();

        separators.put("year", "年");
        separators.put("month", "月");
        separators.put("date", "日");

        separators.put("hour", ":");
        separators.put("minute", ":");
        separators.put("second", ":");

        return separators;
    }

}
相關文章
相關標籤/搜索