1. 程式人生 > >時間型別處理

時間型別處理

import org.apache.commons.lang3.StringUtils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public final class DateUtils {


    /**
     * 日期轉換成字串
     * @param date
     * @param formatStr
     * @return
     */
    public static String DateToString(Date date, String formatStr){
        DateFormat df = new SimpleDateFormat(formatStr);
        return df.format(date);
    }

    /**
     * 日期轉換成yyyy-MM-dd HH:mm:ss字串
     * @param date
     * @return
     */
    public static String getStringDate(Date date){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(date);
    }


    /**
     * 字串轉換成日期
     * @param date
     * @param formatStr
     * @return
     */
    public static Date StringToDate(String date, String formatStr) {
        DateFormat df = new SimpleDateFormat(formatStr);
        try {
            return df.parse(date);
        } catch (ParseException e) {
            //日期格式轉換錯誤
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 時間轉換為時/分/秒
     * @param longTime
     * @return
     */
    public static String TimeToString(long longTime){
        StringBuilder time = new StringBuilder();
        long hours = longTime / 3600;
        long minutes = (longTime % 3600) / 60;
        long seconds = longTime % 60;
        if (hours > 0)
            time.append(hours).append("小時");
        if (minutes > 0)
            time.append(minutes).append("分");
        if (seconds > 0)
            time.append(seconds).append("秒");
        if (hours == 0 && minutes == 0 && seconds == 0)
            time.append("0秒");
        return time.toString();
    }

    /**
     * 字串日期加減
     * @param dateStr   操作日期
     * @param count     加的數量,負數表示減去
     * @param field     單位:Calendar.DATE, Calendar.MONTH, Calendar.YEAR
     * @return          相加後的日期字串
     */
    public static String DateAdd(String dateStr, int count, int field){
        Calendar cd = Calendar.getInstance();
        if(StringUtils.isNotBlank(dateStr)){
            cd.setTime(StringToDate(dateStr, "yyyy-MM-dd"));
            cd.add(field, count);
            dateStr = DateToString(cd.getTime(), "yyyy-MM-dd");
        }
        return dateStr;
    }

    /**
     * 日期相加
     * @param date      操作日期
     * @param count     加的數量,負數表示減去
     * @param field     單位:Calendar.DATE, Calendar.MONTH, Calendar.YEAR
     * @return          相加後的日期
     */
    public static Date DateAdd(Date date, int count, int field){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(field, count);
        return calendar.getTime();
    }


     /**
     * 日期相減獲取時間差(毫秒數)
     * @param startTime
     * @param endTime
     * @param formatStr
     * @return
     */
    public static long DateDiff(String startTime,String endTime,String formatStr){
        long timeDiff = 0;
        if(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)){
            long start_time = StringToDate(startTime,formatStr).getTime();
            long end_time = StringToDate(endTime,formatStr).getTime();
            timeDiff = Math.abs(start_time - end_time);
        }
        return timeDiff;
    }

    /**
     * 獲取今天之前的days天日期列表
     * @param days
     * @return
     */
    public static List<String> getDateList(int days){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        List dateList = new ArrayList();
        for(int i = 0;i<days;i++){
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_MONTH, 0-(i+1));
            String dateStr = DateToString(calendar.getTime(),"yyyy-MM-dd");
            dateList.add(dateStr);
        }
        Collections.reverse(dateList);
        return dateList;
    }

    /**
     * 獲取今天之前的days天日期列表
     * @param days
     * @return
     */
    public static String[] getDateArray(int days){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        String[] dateArray = new String[days];
        for(int i=days;i>0;i--){
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_MONTH, 0-i);
            String dateStr = DateToString(calendar.getTime(),"yyyy-MM-dd");
            dateArray[days-i] = dateStr;
        }
        return dateArray;
    }

    /**
     * 字串日期人性化顯示
     * String formatStr
     * @param date
     * @param formatStr
     * @return
     */
    public static String friendlyTime(String date, String formatStr){
        Date nowdate = StringToDate(DateToString(new Date(),formatStr),formatStr);
        Date ondate = StringToDate(date,formatStr);
        Long diffTime = (nowdate.getTime() - ondate.getTime()) / (86400000);
        String dateStr = "";
        switch (diffTime.intValue()){
            case 0:
                dateStr = "今天";
                break;
            case 1:
                dateStr = "昨天";
                break;
            case 2:
                dateStr = "兩天前";
                break;
            case 3:
                dateStr = "三天前";
                break;
            case 4:
                dateStr = "四天前";
                break;
            case 5:
                dateStr = "五天前";
                break;
            case 6:
                dateStr = "六天前";
                break;
            case 7:
                dateStr = "一週前";
                break;
            default:
                DateFormat df = new SimpleDateFormat(formatStr);
                dateStr = df.format(date);
        }
        return dateStr;
    }

    /**
     * 獲取日期所在的周
     * @param dateStr
     * @param formatStr
     * @return
     */
    public static String weekOfMonth(String dateStr, String formatStr){
        Date date = StringToDate(dateStr,formatStr);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
        return String.valueOf(weekOfMonth);
    }

}