首页 > 学院 > 开发设计 > 正文

两个时间戳相减得到分钟数()

2019-11-09 18:44:50
字体:
来源:转载
供稿:网友

代码如下:

package com.daojishidemo;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.TextView;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.TimeZone;public class MainActivity extends AppCompatActivity {    PRivate int recLen = 0;    private TextView textView;    private int time;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        textView = (TextView) findViewById(R.id.tv_time);        getTime();        new Thread(new MyThread()).start();         // start thread    }    private void getTime() {        TimeZone tz = TimeZone.getTimeZone("GMT+08:00");        TimeZone.setDefault(tz);        SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMdd HH:mm:ss");        formatter.setTimeZone(tz);        Date curDate = new Date(System.currentTimeMillis());//获取当前时间        try {            Date parse = formatter.parse("20170206 12:06:00");//           long  yy =  parse.getTime() -curDate.getTime();            long yy=curDate.getTime()-parse.getTime();            time=new Long(yy/1000/60).intValue();//相差多少分钟        } catch (ParseException e) {            e.printStackTrace();        }    }    final Handler handler = new Handler() {          // handle        public void handleMessage(Message msg) {            int minute=time;            switch (msg.what) {                case 1:                    if (recLen!=0 && minute !=0){                    recLen--;                    textView.setText(minute+":" + recLen);                    }else if (recLen==0 && minute !=0){                        minute--;                        recLen=60;                        recLen--;                        textView.setText(minute+":"+recLen);                    }else if (recLen==0 && minute==0){                        textView.setText(minute +"" +recLen);                    }else if (recLen!=0 && minute==0){                        recLen--;                        textView.setText(minute +":" +recLen);                    }            }            super.handleMessage(msg);        }    };    public class MyThread implements Runnable {      // thread        @Override        public void run() {            while (true) {                try {                   Thread.sleep(1000);     // sleep 1000ms                    Message message = new Message();                    message.what = 1;                    handler.sendMessage(message);                } catch (Exception e) {                }            }        }    }}

2、布局文件

<?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:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    android:gravity="center"    tools:context="com.loupai.daojishidemo.MainActivity">    <TextView        android:id="@+id/tv_time"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="60dp" /></LinearLayout>


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表