两次之间的时差

编程入门 行业动态 更新时间:2024-10-28 10:30:59
本文介绍了两次之间的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想以hh:mm格式显示两次之间的差异.

I want to display the difference between two times in hh:mm format.

第一次是来自数据库,第二次是系统时间.时差每秒更新一次.

The first time is from a database and the second time is the system time. Time difference is updated every second.

我该怎么办?

目前,我正在使用两个手动时间,如果可以正常工作,那么我会将其实现到我的应用中.

Currently I'm using two manual time if this works perfectly then I implement it into my apps.

public class MainActivity extends Activity { TextView mytext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Timer updateTimer = new Timer(); updateTimer.schedule(new TimerTask() { public void run() { try { TextView txtCurrentTime= (TextView)findViewById(R.id.mytext); SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa"); Date date1 = format.parse("08:00:12 pm"); Date date2 = format.parse("05:30:12 pm"); long mills = date1.getTime() - date2.getTime(); Log.v("Data1", ""+date1.getTime()); Log.v("Data2", ""+date2.getTime()); int hours = (int) (mills/(1000 * 60 * 60)); int mins = (int) (mills % (1000*60*60)); String diff = hours + ":" + mins; // updated value every1 second txtCurrentTime.setText(diff); } catch (Exception e) { e.printStackTrace(); } } }, 0, 1000); } }

推荐答案

要计算两个日期之间的差,您可以尝试以下方法:

To Calculate the difference between two dates you could try something like:

long mills = date1.getTime() - date2.getTime(); int hours = millis/(1000 * 60 * 60); int mins = (mills/(1000*60)) % 60; String diff = hours + ":" + mins;

要每秒更新时差,您可以使用计时器.

To update the Time Difference every second you can make use of Timer.

Timer updateTimer = new Timer(); updateTimer.schedule(new TimerTask() { public void run() { try { long mills = date1.getTime() - date2.getTime(); int hours = millis/(1000 * 60 * 60); int mins = (mills/(1000*60)) % 60; String diff = hours + ":" + mins; // updated value every1 second } catch (Exception e) { e.printStackTrace(); } } }, 0, 1000); // here 1000 means 1000 mills i.e. 1 second

工作代码:

public class MainActivity extends Activity { private TextView txtCurrentTime; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtCurrentTime= (TextView)findViewById(R.id.mytext); Timer updateTimer = new Timer(); updateTimer.schedule(new TimerTask() { public void run() { try { SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa"); Date date1 = format.parse("08:00:12 pm"); Date date2 = format.parse("05:30:12 pm"); long mills = date1.getTime() - date2.getTime(); Log.v("Data1", ""+date1.getTime()); Log.v("Data2", ""+date2.getTime()); int hours = (int) (mills/(1000 * 60 * 60)); int mins = (int) (mills/(1000*60)) % 60; String diff = hours + ":" + mins; // updated value every1 second txtCurrentTime.setText(diff); } catch (Exception e) { e.printStackTrace(); } } }, 0, 1000); }

更多推荐

两次之间的时差

本文发布于:2023-10-27 03:53:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532232.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   时差

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!