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
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.
Never use the legacy classes Date
, Calendar
, SimpleDateFormat
, etc. These were years ago supplanted by the modern java.time classes defined in JSR 310, and built into Java 8 and later.
Always use java.time classes for your date-time handling.
Duration
To store a span-of-time unattached to the timeline, on a scale of generic 24-hour days, hours, minutes, seconds, and nanoseconds, use Duration
class.
Duration duration = Duration.ofHours( 5 ).plusMinutes( 4 ).plusSeconds( 3 );
Generate a standard ISO 8601 formatted text.
String iso8601Text = duration.toString() ;
Parse such standard text.
Duration duration = Duration.parse ( "PT5H4M3S" ) ;
You can specify days as well, but these are days unrelated to the calendar. They are generic chunks of 24-hours. For example, specifying two of these “days” is exactly the same as specifying forty-eight hours.
Duration duration = Duration.ofDays( 2 ) ; // 48 hours, not calendar days.
Duration theSame = Duration.ofHours( 48 ) ; // 2 days.
You will find methods such as Thread.sleep
have been updated to accept Duration
object as an argument.
Period
If you wish to represent a span-of-time on the scale of years-months-days, use Period
class.
org.threeten.extra.PeriodDuration
Combining years-months-days with hours-minutes-seconds-nanos does not really make sense, if you think about it. But if you insist on going that route, see the PeriodDuration
class in the ThreeTen-Extra library.
ScheduledExecutorService
By the way, the Timer
& TimerTask
classes are now legacy, as mentioned in their Javadoc.
In Java 5+, we got the Executors framework to relieve us of the chore of juggling threads. Define your task as a Runnable
or Callable
. Then submit an object of that type to an ExecutorService
to be run.
In particular, to schedule a task to run after a delay, use ScheduledExecutorService
. You can even ask that interface to run a task repeatedly, with a specified gap of time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With