Android 中常用的時(shí)間轉(zhuǎn)換集合

在我們的日常開發(fā)中經(jīng)常會(huì)碰到需要時(shí)間轉(zhuǎn)換,這里我把常用的時(shí)間轉(zhuǎn)換方法封裝成一個(gè)TimeUtils,做項(xiàng)目的時(shí)候放進(jìn)項(xiàng)目里直接調(diào)用其中方法就行了,很方便。
廢話不多說,直接上代碼

package com.***.***.util
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 *
 * ClassName:TimeUtils 通用時(shí)間類
 *
 * @author walle
 * @version 1.0
 * @since Ver 1.0
 *
 */
public class TimeUtils {
    public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat DATE_FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat File_DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
    public static final SimpleDateFormat DATE_FORMAT_DATE_NO_YEAR = new SimpleDateFormat("MM-dd");

    /**
     * 將毫秒時(shí)間轉(zhuǎn)換為String類型
     *
     * @param timeInMillis
     *            毫秒時(shí)間
     * @param dateFormat
     *            時(shí)間格式
     * @return
     */
    public static String getTime(long timeInMillis, SimpleDateFormat dateFormat) {
        return dateFormat.format(new Date(timeInMillis));
    }

    /**
     * 將毫秒時(shí)間轉(zhuǎn)換為String類型 默認(rèn)時(shí)間格式為(yyyy-MM-dd HH:mm:ss)
     *
     * @param timeInMillis
     *            毫秒時(shí)間
     * @return
     */
    public static String getTime(long timeInMillis) {
        return getTime(timeInMillis, DEFAULT_DATE_FORMAT);
    }

    /**
     * 獲取當(dāng)前的毫秒時(shí)間
     *
     * @return 毫秒時(shí)間
     */
    public static long getCurrentTimeInLong() {
        return System.currentTimeMillis();
    }

    /**
     * 獲取當(dāng)前時(shí)間,默認(rèn)時(shí)間格式為 yyyy-MM-dd HH:mm:ss
     *
     * @return 當(dāng)前時(shí)間
     */
    public static String getCurrentTimeInString() {
        return getTime(getCurrentTimeInLong());
    }

    /**
     * 獲取當(dāng)前日期,默認(rèn)日期格式為 yyyy-MM-dd
     *
     * @return 當(dāng)前日期
     */
    public static String getCurrentDateInString() {
        return getTime(getCurrentTimeInLong(), DATE_FORMAT_DATE);
    }

    /**
     *
     * getCurrentTimeInString:(根據(jù)毫秒時(shí)間獲取當(dāng)前時(shí)間)
     *
     * @param dateFormat
     *            時(shí)間格式
     * @return String 時(shí)間
     * @throws @since
     *             CodingExample Ver 1.0
     */
    public static String getCurrentTimeInString(SimpleDateFormat dateFormat) {
        return getTime(getCurrentTimeInLong(), dateFormat);
    }

    /**
     * 獲取時(shí)間毫秒數(shù)
     *
     * @param time
     * @return
     */
    public static long getTimeInLong(String time) {
        try {
            Date date = DEFAULT_DATE_FORMAT.parse(time);
            return date.getTime();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return 0;
    }

    public static long getTimeInLong(String time, SimpleDateFormat format) {
        try {
            Date date = format.parse(time);
            return date.getTime();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return 0;
    }

    /**
     * 計(jì)算兩個(gè)時(shí)間段相差毫秒數(shù)
     *
     * @param dateBefore
     * @param dateAfter
     * @return
     */
    public static long getSubTimeInMillionSecond(String dateBefore, String dateAfter) {
        try {
            long before = getTimeInLong(dateBefore);
            long after = getTimeInLong(dateAfter);
            long subValue = after - before;
            return subValue;
        } catch (Exception e) {
            return 0;
        }

    }

    /**
     * 計(jì)算兩個(gè)時(shí)間段相差幾天
     *
     * @param dateBefore
     * @param dateAfter
     * @return
     */
    public static long getSubDays(String dateBefore, String dateAfter) {
        long milliSecond = getSubTimeInMillionSecond(dateBefore, dateAfter);
        long subDays = milliSecond / (1000 * 60 * 60 * 24);
        return subDays;
    }

    

    public static String getCurDate() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mYear = String.valueOf(c.get(Calendar.YEAR)); // 獲取當(dāng)前年份
        String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 獲取當(dāng)前月份
        String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 獲取當(dāng)前月份的日期號(hào)碼
        return mYear + "年" + mMonth + "月" + mDay + "日";
    }

    public static String getWeekOfDate() {
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance();
        Date curDate = new Date(System.currentTimeMillis());
        cal.setTime(curDate);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

    public static String getWeekIndexOfDate() {
        Calendar cal = Calendar.getInstance();
        Date curDate = new Date(System.currentTimeMillis());
        cal.setTime(curDate);
        int w = cal.get(Calendar.WEEK_OF_YEAR);
        if (w < 0) {
            w = 0;
        }
        return String.valueOf(w);
    }

}

在做項(xiàng)目的時(shí)候創(chuàng)建一個(gè)TimeUtils類,將以上代碼復(fù)制進(jìn)去,在需要的地方調(diào)用,非常方便,節(jié)約了重復(fù)查找的時(shí)間。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,552評(píng)論 25 708
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,991評(píng)論 19 139
  • 重點(diǎn)提煉: 1、提問不是重要方法,而是唯一方法 2、單詞是有音節(jié)組成的——自然拼讀 3、英語是一種形合語言
    漫步和蝸牛閱讀 111評(píng)論 0 0
  • 突然很好奇別人有沒有自己的種種感受,嘗試搜一搜 “心如刀絞”,壓抑的時(shí)候會(huì)有這種感覺,好像是遺傳因素,情緒激烈的時(shí)...
    體溫365閱讀 205評(píng)論 0 1
  • git安裝完后全局配置一下用戶名,郵箱 簡(jiǎn)單步驟
    當(dāng)如初見_163a閱讀 104評(píng)論 0 0