Android 倒计时(时间戳转换天小时分钟秒)
Posted 亮亮在江湖
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 倒计时(时间戳转换天小时分钟秒)相关的知识,希望对你有一定的参考价值。
最近在做类似于商品购买的时间倒计时,在此记录一下,话不多少,请看效果图:
倒计时 = 从后台获取到当前时间的时间戳 - 商品的截止时间
(备注:如果当前时间的时间戳获取手机本地的会出问题,因为调整本地时间后就造成的时间错误,所以线上环境每次取从后台获取到当前时间的时间戳)
此处涉及到时间戳和日期年月日时分秒的转换
//将时间戳转化为对应的时间 日-时-分-秒
public static String timeConversion(long time)
long day = 0;
long hour = 0;
long minutes = 0;
long sencond = 0;
long dayTimp = time % (3600*24);
long hourTimp = time % 3600;
if(time >= 86400)
day = time / (3600*24);
if(dayTimp != 0)
time = time-(day * 24 * 60 * 60);
if(time >= 3600 && time < 86400)
hour = time / 3600;
if (hourTimp != 0)
if (hourTimp >= 60)
minutes = hourTimp / 60;
if (hourTimp % 60 != 0)
sencond = hourTimp % 60;
else if (hourTimp < 60)
sencond = hourTimp;
else if(time < 3600)
minutes = time / 60;
if (time % 60 != 0)
sencond = time % 60;
else if (time >= 3600 && time < 86400)
hour = time / 3600;
if (hourTimp != 0)
if (hourTimp >= 60)
minutes = hourTimp / 60;
if (hourTimp % 60 != 0)
sencond = hourTimp % 60;
else if (hourTimp < 60)
sencond = hourTimp;
else if(time < 3600)
minutes = time / 60;
if (time % 60 != 0)
sencond = time % 60;
return (day<10?("0"+day):day) + "天" + (hour<10?("0"+hour):hour) + "时" + (minutes<10?("0"+minutes):minutes) + "分" + (sencond<10?("0"+sencond):sencond)+ "秒";
//将时间字符串转为时间戳字符串
public static Long getStringTimestamp(String time)
Long longTime = null;
try
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
longTime = sdf.parse(time).getTime() / 1000;
catch (ParseException e)
e.printStackTrace();
return longTime;
通过handler 发消息,每隔1秒钟发送一次,时间递减
@SuppressLint("HandlerLeak")
private Handler handler = new Handler()
@Override
public void handleMessage (Message msg)
super.handleMessage(msg);
long sysTime = System.currentTimeMillis();
String sysTimeStr = (String) DateFormat.format("yyyy-MM-dd HH:mm:ss", sysTime);
tv_time.setText("当前时间-----"+sysTimeStr);
Long currentStamp = getStringTimestamp(sysTimeStr);
Long currentLast = endStamp - currentStamp;
if(currentLast <= 0)
handler.removeMessages(0);
tv_last_time.setText("购买时间截止");
return;
String lastTime = timeConversion(currentLast);
SpannableStringBuilder ss = new SpannableStringBuilder();
ss.append("购买截止时间:"+lastTime);
ss.setSpan(new ForegroundColorSpan(ContextCompat.getColor(MainActivity.this,R.color.color_gold)), 7, 9, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new ForegroundColorSpan(ContextCompat.getColor(MainActivity.this,R.color.color_gold)), 10, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new ForegroundColorSpan(ContextCompat.getColor(MainActivity.this,R.color.color_gold)), 13, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new ForegroundColorSpan(ContextCompat.getColor(MainActivity.this,R.color.color_gold)), 16, 18, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv_last_time.setText(ss);
handler.sendEmptyMessageDelayed(0, 1000);
;
最后在页面销毁时移除掉handler
@Override
protected void onDestroy()
super.onDestroy();
handler.removeCallbacksAndMessages(null);
@Override
protected void onPause()
super.onPause();
handler.removeMessages(0);
希望本篇文章对于有需要的同学给与帮助,欢迎讨论!附上代码demo
以上是关于Android 倒计时(时间戳转换天小时分钟秒)的主要内容,如果未能解决你的问题,请参考以下文章