1. 程式人生 > >Android獲取各式時間型別

Android獲取各式時間型別

在專案中需要獲取各式各樣的時間,而且也會有很多地方會呼叫到這些方法,所以我將它們歸集於一個檔案中,方便之後的使用。


現在我們需要了解一些相對基礎獲取時間的方法。

1.獲取當前日曆物件:

Calendar calendar = Calendar.getInstance();
2.獲取當前時區下日期時間對應的時間戳:

calendar.getTimeInMillis();

3.獲取標準格林尼治時間下日期時間對應的時間戳:

long unixTime = calendar.getTimeInMillis();
unixTime - TimeZone.getDefault().getRawOffset();

4.獲取當前日期物件:

Date date = new Date();

5.獲取當前時區下日期時間對應的時間戳:

date.getTimeInMillis();

6.設定日期時間格式:

SimpleDateFormat format = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

7.獲取當前時區下日期時間對應的時間戳:

format.format(date);


現在來實現具體的方法。

1.獲取時間戳:

 public static long getTime() {
        Calendar calendar = Calendar.getInstance();// 獲取當前日曆物件
        long unixTime = calendar.getTimeInMillis();// 獲取當前時區下日期時間對應的時間戳
        return unixTime;
    }

2.獲取標準的時間:

 public static String getStandardTime() {
        SimpleDateFormat formatter = new SimpleDateFormat(BaseApplication.getInstance().getString(R.string.date_show_type_one));
        Date curDate = new Date(System.currentTimeMillis());// 獲取當前時間
        return formatter.format(curDate);
    }

3.獲取與現在時間的時間差(秒):

public static int getDurationSecond(String time) {
        int durationSecond = 0;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date;
        try {
            date = df.parse(time);
            MyLog.i("TimeUtils getDurationSecond Date=" + new Date().toString());
            durationSecond = (int) ((new Date().getTime() - date.getTime()) / 1000);
        } catch (Exception e) {
            MyLog.e("TimeUtils getDurationSecond error=" + e);
        }
        return durationSecond;
    }

4.獲取時間差:

public static String getDuration(String one, String two) {
        String duration = "";
        SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span>);
        Date date1;
        Date date2;
        try {
            date1 = df.parse(one);
            date2 = df.parse(two);
            int l = (int) ((date2.getTime() - date1.getTime()) / 1000 / 60);
            if (l > 60) {
                int hr = l / 60;
                int min = l % 60;
                duration = hr + "小時" + min + "分鐘";
            } else {
                duration = l + "分鐘";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return duration;
    }


完整程式碼展示:

public class MyTimeUtils {
  
    //獲取時間戳
    public static long getTime() {
        Calendar calendar = Calendar.getInstance();// 獲取當前日曆物件
        long unixTime = calendar.getTimeInMillis();// 獲取當前時區下日期時間對應的時間戳
        return unixTime;
    }

    public static String getTimeString() {
        return Long.toString(new Date().getTime());
    }

    //獲取標準時間
    public static String getStandardTime() {
        SimpleDateFormat formatter = new SimpleDateFormat(BaseApplication.getInstance().getString(R.string.date_show_type_one));
        Date curDate = new Date(System.currentTimeMillis());// 獲取當前時間
        return formatter.format(curDate);
    }

    // 獲取與現在時間的時間差(秒)
    public static int getDurationSecond(String time) {
        int durationSecond = 0;
        SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span>);
        Date date;
        try {
            date = df.parse(time);
            MyLog.i("TimeUtils getDurationSecond Date=" + new Date().toString());
            durationSecond = (int) ((new Date().getTime() - date.getTime()) / 1000);
        } catch (Exception e) {
            MyLog.e("TimeUtils getDurationSecond error=" + e);
        }
        return durationSecond;
    }


    // 獲取時間差
    public static String getDuration(String one, String two) {
        String duration = "";
        SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span><span style="font-family: SimHei;">);</span>
        Date date1;
        Date date2;
        try {
            date1 = df.parse(one);
            date2 = df.parse(two);
            int l = (int) ((date2.getTime() - date1.getTime()) / 1000 / 60);
            if (l > 60) {
                int hr = l / 60;
                int min = l % 60;
                duration = <span style="font-family: SimHei;">hr + "小時" + min + "分鐘"</span>;
            } else {
                duration = <span style="font-family: SimHei;">l + "分鐘";</span>
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return duration;
    }

    // 獲取與當前時間差
    public static String getcurDuration(String one) {
        String duration = "";
        SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span>);
        Date date1;
        Date date2;
        try {
            date1 = df.parse(one);
            date2 = new Date();
            int l = (int) ((date2.getTime() - date1.getTime()) / 1000 / 60);
            if (l > 60) {
                int hr = l / 60;
                int min = l % 60;
                duration = <span style="font-family: SimHei;">hr + "小時" + min + "分鐘"</span><span style="font-family: SimHei;">;</span>
            } else {
                duration =<span style="font-family: SimHei;"> l + "分鐘";</span>

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return duration;
    }

    /**
     * @return格式化當前日期和時間為字串
     */
    public static String mCurrentTime() {
        SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span>);
        String currenttime = df.format(new Date());
        return currenttime;
    }

    public static String parseBangTime(long time) {
        MyLog.out("time==>" + time);
        String timeTemp = "";
        if (time < 60) {
            timeTemp = time + BaseApplication.getInstance().getString(R.string.seconds_before);
        } else if (time < (60 * 60)) {
            timeTemp = time / 60 + BaseApplication.getInstance().getString(R.string.minutes_before);
        } else if (time < (3600 * 24)) {
            timeTemp = time / 3600 + BaseApplication.getInstance().getString(R.string.hour_before);
        } else if (time < (60 * 60 * 24 * 30)) {
            timeTemp = time / (3600 * 24) + BaseApplication.getInstance().getString(R.string.today_before);
        } else {
            timeTemp = time / (3600 * 24 * 30) + BaseApplication.getInstance().getString(R.string.month_before);
        }
        return timeTemp;
    }

    public static String getTimeStamp() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(BaseApplication.getInstance().getString(R.string.date_show_type_two));
        String timeStamp = dateFormat.format(new Date());
        MyLog.e("getTimeStamp=" + timeStamp);
        return timeStamp;
    }

    public static String getCurrentDate(){
        SimpleDateFormat df = new SimpleDateFormat(BaseApplication.getInstance().getString(R.string.date_show));
        String currentDate = df.format(new Date());
        return currentDate;
    }
}