Android如何显示优惠结束时间计数器?(Android How to show offers end time counter?)
我试图在我的应用程序中显示一些提供结束时间计数器,它从我的后端获取结束时间字符串,并假设显示剩余天数:小时:分钟:通过将它与当前日期进行比较但我真的迷路了!
我希望有人能够通过它
她是我的代码:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); String offerenddate = "2017-04-01 12:56:50"; final SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); try { Date oldDate = dateFormat.parse(offerenddate); Date currentDate = new Date(); long diff = oldDate.getTime() - currentDate.getTime(); long seconds = diff / 1000; final long minutes = seconds / 60; final long hours = minutes / 60; final long days = hours / 24; new CountDownTimer( diff,1000) { @Override public void onTick(long l) { tv.setText("Time Remaining : "+days+" - "+hours+":"+minutes); } @Override public void onFinish() { tv.setText("Closed"); } }.start(); } catch (Exception e) { e.printStackTrace(); } } }I'm trying to show some offer end time counter in my app which takes the end time string from my backend and its suppose to display the remaining days:hours:minutes:seconds by comparing it with current date but i really get lost !!
i hope that some one go through it
her is my code:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); String offerenddate = "2017-04-01 12:56:50"; final SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); try { Date oldDate = dateFormat.parse(offerenddate); Date currentDate = new Date(); long diff = oldDate.getTime() - currentDate.getTime(); long seconds = diff / 1000; final long minutes = seconds / 60; final long hours = minutes / 60; final long days = hours / 24; new CountDownTimer( diff,1000) { @Override public void onTick(long l) { tv.setText("Time Remaining : "+days+" - "+hours+":"+minutes); } @Override public void onFinish() { tv.setText("Closed"); } }.start(); } catch (Exception e) { e.printStackTrace(); } } }最满意答案
您可以使用Library,countdownView https://github.com/iwgang/CountdownView 。
You may use the Library,countdownView https://github.com/iwgang/CountdownView.
更多推荐
发布评论