Display elapsed time with month an day support in JAVA

…衆ロ難τιáo~ 提交于 2019-12-20 03:42:13

问题


I need to display elapsed time in my application form.
I currently do this using this code:

    AppTimer = new Timer();
    AppTimer.scheduleAtFixedRate(new java.util.TimerTask() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        public void run() {
            sdf.setTimeZone(java.util.TimeZone.getTimeZone("GMT+0"));
            lblAppRunTime.setText(""+sdf.format(new Date(AppTimeCounter*1000)));
            AppTimeCounter++;
        }
    },1000, 1000);

This Code shows hours,minutes & seconds properly but how can I show DAYS & MONTHS? new SimpleDateFormat("M D HH:mm:ss") can't do this and for example when 5 seconds elapsed it shows 1 month and 1 days too!

How can I show month and day too?

Thanks


回答1:


You should not use the Date class for it, because a date is an instant of time, but you are using it as a duration. Please read carefully joda time documentation about it. And then you could use Period and PeriodFormatter for you task. Or write something similar if you don't want use the library.



来源:https://stackoverflow.com/questions/7807119/display-elapsed-time-with-month-an-day-support-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!