常用代碼整理:時間工具類(TimeUtil)

說明:大部分內容都是參考別的文章,這裏做整理是爲了以後的編程有實用的模板,可以即需即用。

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {

    /**
     * 默認是 yyyy-MM-dd HH:mm:ss
     * 如果要其他格式可以通過 TimeUtil.SHORT_SDF 直接引用
     * 也可以自己寫 "yyyy-MM-dd HH"
     */
    public final static SimpleDateFormat SHORT_SDF = new SimpleDateFormat("yyyy-MM-dd");
    public final static SimpleDateFormat LONG_SDF_DEFAULT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 按默認格式轉換 millis2String
     */
    public static String millis2String(long millis) {
        return millis2String(millis, LONG_SDF_DEFAULT);
    }

    /**
     * 按指定格式轉換 millis2String
     */
    public static String millis2String(long millis, DateFormat format) {
        return format.format(new Date(millis));
    }

    /**
     * 按默認格式轉換 string2Millis
     */
    public static long string2Millis(String time) {
        return string2Millis(time, LONG_SDF_DEFAULT);
    }

    /**
     * 按指定格式轉換 string2Millis
     */
    public static long string2Millis(String time, DateFormat format) {
        try {
            return format.parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 按默認格式轉換 string2Date
     */
    public static Date string2Date(String time) {
        return string2Date(time, LONG_SDF_DEFAULT);
    }

    /**
     * 按指定格式轉換 string2Date
     */
    public static Date string2Date(String time, DateFormat format) {
        try {
            return format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 按默認格式轉換 date2String
     */
    public static String date2String(Date date) {
        return date2String(date, LONG_SDF_DEFAULT);
    }

    /**
     * 按指定格式轉換 string2Date
     */
    public static String date2String(Date date, DateFormat format) {
        return format.format(date);
    }

    /**
     * date2String
     */
    public static long date2Millis(Date date) {
        return date.getTime();
    }

    /**
     * millis2Date
     */
    public static Date millis2Date(long millis) {
        return new Date(millis);
    }

    /*
     * 如果能通過系統API能簡單獲取,爲什麼還要寫入工具類?!
     * 如果寫入工具類,以後要改就麻煩了,所以能不寫入就不寫
     */
    /*public static long getCurrentTimeInLong() {
        return System.currentTimeMillis();
    }*/

    /**
     * 獲取給定天數前的日期
     */
    public static String getDateBefore(int days) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -days);
        return SHORT_SDF.format(cal.getTime());
    }

    /**
     * 獲取給定天數後的日期
     */
    public static String getDateAfter(int days) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, days);
        return SHORT_SDF.format(cal.getTime());
    }

    /**
     * 如果在1分鐘之內發佈的顯示 "剛剛" 如果在1個小時之內發佈的顯示 "XX分鐘之前" 如果在1天之內發佈的顯示 "XX小時之前"
     * 如果在今年的1天之外的只顯示 “月-日”,例如 “05-03” 如果不是今年的顯示“年-月-日”,例如 “2000-01-01”
     */
    public static String getTranslateTime(String time) {
        Date nowTime = new Date();
        String currDate = LONG_SDF_DEFAULT.format(nowTime);
        long currentMilliseconds = nowTime.getTime();
        long timeMilliseconds = 0;
        Date date = null;
        try {
            // 將給定的字符串中的日期提取出來
            date = LONG_SDF_DEFAULT.parse(time);
        } catch (Exception e) {
            e.printStackTrace();
            return time;
        }
        if (date != null) {
            timeMilliseconds = date.getTime();
        }

        long timeDifferent = currentMilliseconds - timeMilliseconds;
        if (timeDifferent < 60000) {
            return "剛剛";
        }
        if (timeDifferent < 3600000) {
            long longMinute = timeDifferent / 60000;
            int minute = (int) (longMinute % 100);
            return minute + "分鐘之前";
        }
        long l = 24 * 60 * 60 * 1000; // 每天的毫秒數
        if (timeDifferent < l) {
            long longHour = timeDifferent / 3600000;
            int hour = (int) (longHour % 100);
            return hour + "小時之前";
        }
        if (timeDifferent >= l) {
            String currYear = currDate.substring(0, 4);
            String year = time.substring(0, 4);
            if (!year.equals(currYear)) {
                return time.substring(0, 10);
            }
            return time.substring(5, 10);
        }
        return time;
    }

}

參考文章:
1、Lazy/TimeUtils.java at master · l123456789jy/Lazy · GitHub
2、AndroidUtilCode/TimeUtils.java at master · Blankj/AndroidUtilCode · GitHub
3、https://www.cnblogs.com/realshijing/p/8419421.html
4、https://blog.csdn.net/u012811342/article/details/78853236

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