如何更新每一秒时间用Java模拟器? [英] How to update time every second in Java emulator?
本文介绍了如何更新每一秒时间用Java模拟器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在code为一个应用程序,但是当我运行code我不能在模拟器中更新的时间。在code工作在编译器而不是模拟器。
任何有帮助的建议将是极大的AP preciated。定时器是倒数圣诞节。
这里是我的code:
今天日历= Calendar.getInstance();
日历thatDay = Calendar.getInstance();
thatDay.set(Calendar.DAY_OF_MONTH,25);
thatDay.set(的Calendar.MONTH,11); // 0-11所以少1
thatDay.set(Calendar.YEAR,2014年);
thatDay.set(Calendar.HOUR,0);
thatDay.set(Calendar.MINUTE,0);
thatDay.set(Calendar.SECOND,0);
thatDay.set(Calendar.AM_PM,0);的System.out.println(thatDay.getTime());ScheduledExecutorService的ScheduledExecutorService的=Executors.newScheduledThreadPool(1);
scheduledExecutorService.scheduleAtFixedRate(新ReadThisPeriod(thatDay),0,1,
TimeUnit.SECONDS);长差异=(thatDay.getTimeInMillis() - today.getTimeInMillis())/ 1000;
长天=差异/(60 * 60 * 24);
时间长=差异/(60 * 60)%24;
长期分钟=差异/ 60%60;
长秒=差异%60;TextView的daysBox =(的TextView)findViewById(R.id.s1Days);daysBox.setText(++日++时间++分++秒+);
解决方案
也有一些小的添加到 rupps 答案。您可以使用
DateUtils.getRelativeTimeSpanString(长时间,现在,长minResolution);
要简化数学和给予更多的用户可读的字符串用户。
I have been working on a code for an app but I cannot get the time to update in the emulator when I run the code. The code works in the compiler but not the emulator. Any helpful suggestions would be greatly appreciated. The timer is a countdown to Christmas day. Here is my code:
Calendar today = Calendar.getInstance();
Calendar thatDay = Calendar.getInstance();
thatDay.set(Calendar.DAY_OF_MONTH, 25);
thatDay.set(Calendar.MONTH, 11); // 0-11 so 1 less
thatDay.set(Calendar.YEAR, 2014);
thatDay.set(Calendar.HOUR, 0);
thatDay.set(Calendar.MINUTE, 0);
thatDay.set(Calendar.SECOND, 0);
thatDay.set(Calendar.AM_PM, 0);
System.out.println(thatDay.getTime());
ScheduledExecutorService scheduledExecutorService=
Executors.newScheduledThreadPool(1);
scheduledExecutorService.scheduleAtFixedRate(new ReadThisPeriod(thatDay), 0, 1,
TimeUnit.SECONDS);
long diff = (thatDay.getTimeInMillis() - today.getTimeInMillis()) / 1000;
long days = diff / (60 * 60 * 24);
long hours = diff / (60 * 60) % 24;
long minutes = diff / 60 % 60;
long seconds = diff % 60;
TextView daysBox = (TextView) findViewById(R.id.s1Days);
daysBox.setText(" + "" + days + "" + hours + "" + minutes + " " + seconds + " ");
解决方案
Also some little add to rupps answer. You can use
DateUtils.getRelativeTimeSpanString(long time, long now, long minResolution);
to simplify math and give more user readable string for user.
这篇关于如何更新每一秒时间用Java模拟器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文