真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Android進(jìn)階之使用時(shí)間戳計(jì)算時(shí)間差

本文實(shí)例為大家分享了Android使用時(shí)間戳計(jì)算時(shí)間差的具體代碼,供大家參考,具體內(nèi)容如下

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、微信小程序開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了金牛免費(fèi)建站歡迎大家使用!

因當(dāng)前項(xiàng)目需要計(jì)算時(shí)間差,進(jìn)行數(shù)據(jù)處理,所以在Csdn上找了一下,之后修修補(bǔ)補(bǔ)是可以用的,建議大家如果用到項(xiàng)目中的話,可能需要把老的時(shí)間戳或者時(shí)間format存儲(chǔ)在文件或者sp中,之后用于判斷,然后進(jìn)行自己的邏輯處理。

Effect :

Android進(jìn)階之使用時(shí)間戳計(jì)算時(shí)間差

Log執(zhí)行:

Android進(jìn)階之使用時(shí)間戳計(jì)算時(shí)間差

注:這個(gè)可以自己簡(jiǎn)單封裝下,比較簡(jiǎn)單。

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");//后的時(shí)間
    Date d2 = format.parse("2012-11-04 11:10:00"); //前的時(shí)間
    Long diff = d1.getTime() - d2.getTime(); //兩時(shí)間差,精確到毫秒

    Long day = diff / (1000 * 60 * 60 * 24);   //以天數(shù)為單位取整
    Long hour=(diff/(60*60*1000)-day*24);    //以小時(shí)為單位取整
    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();
    }
   }
  });

  //獲取當(dāng)前的時(shí)間戳和時(shí)間轉(zhuǎn)譯 - 這里同時(shí)用存儲(chǔ)老的時(shí)間
  mBtnNow.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
    Toast.makeText(MainActivity.this,"獲取當(dāng)前時(shí)間",Toast.LENGTH_SHORT).show();
    long timeMillis = System.currentTimeMillis();
    Log.e("tag timeMillis =",""+timeMillis);
    //將時(shí)間戳轉(zhuǎn)為日期格式
    String time = stampToDate(timeMillis);
    Log.e("tag time = ",time);
    oldtime=time;
    Log.e("tag newtime = ",oldtime);
    try {
     //將日期格式轉(zhuǎn)回為時(shí)間戳的格式
     String what = dateToStamp(time);
     Log.e("tag what = ",what);
    } catch (ParseException e) {
     e.printStackTrace();
    }

   }
  });

  //This is my code - - 主要作用與計(jì)算時(shí)間差 (會(huì)用到之前我們的記錄的時(shí)間,所以使用的時(shí)候,無(wú)比先執(zhí)行上面的邏輯)
  mCount.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
    //思維方式,使用最新的時(shí)間減去之前我們的老時(shí)間進(jìn)行運(yùn)算

    Toast.makeText(MainActivity.this,"新老時(shí)間觸發(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 {
    //嚴(yán)格來(lái)講,在使用中這里需要判斷的,尤其是null的判斷,這里我們使用的了 try catch

    Date d1 = format.parse(newtime); //當(dāng)前時(shí)間
    Date d2 = format.parse(oldtime); //之前記錄的時(shí)間

    Long diff = d1.getTime() - d2.getTime(); //兩時(shí)間差,精確到毫秒
     //以天數(shù)為單位取整
     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();
    }
   }
  });
 }


  /**
  * 將時(shí)間轉(zhuǎn)換為時(shí)間戳
  */
 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);
 }

  /**
  * 將時(shí)間戳轉(zhuǎn)換為時(shí)間
  */
 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"?>


 

 

 
 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。


網(wǎng)頁(yè)標(biāo)題:Android進(jìn)階之使用時(shí)間戳計(jì)算時(shí)間差
URL分享:http://weahome.cn/article/gcchee.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部