Android進階之使用時間戳計算時間差
本文實例為大家分享了Android使用時間戳計算時間差的具體代碼,供大家參考,具體內(nèi)容如下
因當前項目需要計算時間差,進行數(shù)據(jù)處理,所以在Csdn上找了一下,之后修修補補是可以用的,建議大家如果用到項目中的話,可能需要把老的時間戳或者時間format存儲在文件或者sp中,之后用于判斷,然后進行自己的邏輯處理。
Effect :
Log執(zhí)行:
注:這個可以自己簡單封裝下,比較簡單。
MainActivity :
package com.bakheet.effect.time; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.TextView; import android.widget.Toast; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class MainActivity extends AppCompatActivity { private SimpleDateFormat format; public String oldtime ; public String newtime; private TextView mContent; private TextView mCount; private TextView mBtnNow; private TextView mBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); mBtn = (TextView) findViewById(R.id.btn); mBtnNow = (TextView) findViewById(R.id.btn_now); mCount = (TextView) findViewById(R.id.count); mContent = (TextView) findViewById(R.id.content); //Csdn內(nèi)一篇博主的博文 mBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this,"Csdn博友事件觸發(fā)",Toast.LENGTH_SHORT).show(); try { Date d1 = format.parse("2012-11-05 12:00:00");//后的時間 Date d2 = format.parse("2012-11-04 11:10:00"); //前的時間 Long diff = d1.getTime() - d2.getTime(); //兩時間差,精確到毫秒 Long day = diff / (1000 * 60 * 60 * 24); //以天數(shù)為單位取整 Long hour=(diff/(60*60*1000)-day*24); //以小時為單位取整 Long min=((diff/(60*1000))-day*24*60-hour*60); //以分鐘為單位取整 Long second=(diff/1000-day*24*60*60-hour*60*60-min*60);//秒 Log.e("tag","day =" +day); Log.e("tag","hour =" +hour); Log.e("tag","min =" +min); Log.e("tag","second =" +second); mContent.setText("day = "+day+",hour = "+hour+",min = "+min+",second = "+second); } catch (Exception e) { e.printStackTrace(); } } }); //獲取當前的時間戳和時間轉(zhuǎn)譯 - 這里同時用存儲老的時間 mBtnNow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this,"獲取當前時間",Toast.LENGTH_SHORT).show(); long timeMillis = System.currentTimeMillis(); Log.e("tag timeMillis =",""+timeMillis); //將時間戳轉(zhuǎn)為日期格式 String time = stampToDate(timeMillis); Log.e("tag time = ",time); oldtime=time; Log.e("tag newtime = ",oldtime); try { //將日期格式轉(zhuǎn)回為時間戳的格式 String what = dateToStamp(time); Log.e("tag what = ",what); } catch (ParseException e) { e.printStackTrace(); } } }); //This is my code - - 主要作用與計算時間差 (會用到之前我們的記錄的時間,所以使用的時候,無比先執(zhí)行上面的邏輯) mCount.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //思維方式,使用最新的時間減去之前我們的老時間進行運算 Toast.makeText(MainActivity.this,"新老時間觸發(fā)",Toast.LENGTH_SHORT).show(); long timeMillis = System.currentTimeMillis(); Log.e("tag timeMillis =",""+timeMillis); String time = stampToDate(timeMillis); Log.e("tag time = ",time); newtime=time; Log.e("tag newtime = ",newtime); try { //嚴格來講,在使用中這里需要判斷的,尤其是null的判斷,這里我們使用的了 try catch Date d1 = format.parse(newtime); //當前時間 Date d2 = format.parse(oldtime); //之前記錄的時間 Long diff = d1.getTime() - d2.getTime(); //兩時間差,精確到毫秒 //以天數(shù)為單位取整 Long day = diff / (1000 * 60 * 60 * 24); //以小時為單位取整 Long hour=(diff/(60*60*1000)-day*24); //以分鐘為單位取整 Long min=((diff/(60*1000))-day*24*60-hour*60); //以秒為單位 Long second=(diff/1000-day*24*60*60-hour*60*60-min*60); Log.e("tag","day =" +day); Log.e("tag","hour =" +hour); Log.e("tag","min =" +min); Log.e("tag","second =" +second); mContent.setText("day = "+day+",hour = "+hour+",min = "+min+",second = "+second); } catch (Exception e) { //建議拋出總異常 e.printStackTrace(); } } }); } /** * 將時間轉(zhuǎn)換為時間戳 */ public String dateToStamp(String time) throws ParseException { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = simpleDateFormat.parse(time); long ts = date.getTime(); return String.valueOf(ts); } /** * 將時間戳轉(zhuǎn)換為時間 */ public String stampToDate(long timeMillis){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(timeMillis); return simpleDateFormat.format(date); } }
MainActivity Xml :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bakheet.effect.time.MainActivity"> <TextView android:layout_marginTop="10dp" android:layout_width="match_parent" android:padding="5dp" android:layout_height="wrap_content" android:text="csdn博友時間差算法" android:gravity="center" android:id="@+id/btn" /> <TextView android:layout_marginTop="10dp" android:layout_width="match_parent" android:padding="5dp" android:layout_height="wrap_content" android:gravity="center" android:text="當前時間" android:id="@+id/btn_now" /> <TextView android:layout_marginTop="10dp" android:layout_width="match_parent" android:padding="5dp" android:layout_height="wrap_content" android:gravity="center" android:id="@+id/count" android:text="新老時間計算" /> <TextView android:layout_marginTop="10dp" android:layout_width="match_parent" android:padding="5dp" android:layout_height="wrap_content" android:gravity="center" android:id="@+id/content" android:text="" /> </LinearLayout>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Android使用Gradle統(tǒng)一配置依賴管理
本篇文章主要介紹了詳解Android 使用 Gradle 統(tǒng)一配置依賴管理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01Android RecyclerView設(shè)置下拉刷新的實現(xiàn)方法
這篇文章主要介紹了Android RecyclerView設(shè)置下拉刷新的實現(xiàn)方法,希望通過本文通過SwipeRefreshLayout方式實現(xiàn)下拉刷新,需要的朋友可以參考下2017-10-10Android實現(xiàn)ListView異步加載的方法(改進版)
這篇文章主要介紹了Android實現(xiàn)ListView異步加載的方法,針對前面介紹的方法進行了線程操作的改進,具有一定參考借鑒價值,需要的朋友可以參考下2016-08-08Android 基礎(chǔ)入門教程——開發(fā)環(huán)境搭建
這篇文章主要介紹了Android 如何搭建開發(fā)環(huán)境,文中講解非常細致,幫助大家開始學習Android,想要學習Android的朋友可以了解下2020-06-06Android kotlin使用注解實現(xiàn)防按鈕連點功能的示例
這篇文章主要介紹了Android kotlin使用注解實現(xiàn)防按鈕連點功能的示例,幫助大家更好的理解和學習使用Android,感興趣的朋友可以了解下2021-03-03flutter showModalBottomSheet常用屬性及說明
這篇文章主要介紹了flutter showModalBottomSheet常用屬性及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09Android 使用Vitamio打造自己的萬能播放器(6)——在線播放(播放列表)
本文主要介紹Android Vitamino在線播放列表,這里給大家提供效果圖和實例代碼以便大家參考學習,希望能幫助開發(fā)Android視頻播放的朋友2016-07-07