Android进阶之路 - 使用时间戳计算时间差
程序员文章站
2022-04-16 21:24:10
...
因当前项目需要计算时间差,进行数据处理,所以在Csdn上找了一下,之后修修补补是可以用的,建议大家如果用到项目中的话,可能需要把老的时间戳或者时间format存储在文件或者sp中,之后用于判断,然后进行自己的逻辑处理。
Effect :
Log执行:
注:这个可以自己简单封装下,比较简单。
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内一篇博主的博文
mBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"Csdn博友事件触发",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); //以天数为单位取整
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();
}
}
});
//获取当前的时间戳和时间转译 - 这里同时用存储老的时间
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);
//将时间戳转为日期格式
String time = stampToDate(timeMillis);
Log.e("tag time = ",time);
oldtime=time;
Log.e("tag newtime = ",oldtime);
try {
//将日期格式转回为时间戳的格式
String what = dateToStamp(time);
Log.e("tag what = ",what);
} catch (ParseException e) {
e.printStackTrace();
}
}
});
//This is my code - - 主要作用与计算时间差 (会用到之前我们的记录的时间,所以使用的时候,无比先执行上面的逻辑)
mCount.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);
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(); //两时间差,精确到毫秒
//以天数为单位取整
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();
}
}
});
}
/**
* 将时间转换为时间戳
*/
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);
}
/**
* 将时间戳转换为时间
*/
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>
上一篇: 蛔虫
下一篇: 最搞笑最绝的男女问答