Summary of time dynamic display methods implemented in Java

The example described in this paper can realize the dynamic display time of Java on the interface. The specific implementation methods are summarized as follows:

1. Method 1 uses TimerTask:

Using Java util. Timer and Java util. TimerTask to do dynamic updates. After all, each update can be regarded as occurring once every 1 second. The code is as follows:

Inherit TimerTask to create a custom task, get the current time, and update displayarea Then create an instance of timer and execute TimerTask every 1 second. Because there may be time errors when using schedule, you can directly call scheduleatfixedrate with higher accuracy. 2. Method 2: using threads:

This is relatively simple. The specific codes are as follows:

Comparison:

Personally, I prefer method 1, because the timer can be shared by multiple timertasks to generate a thread, which will increase the maintenance complexity of multiple threads.

Note the following codes:

After the above method is slightly modified, the Multi Country time can be displayed. The code is as follows:

The displayarea needs to be updated in updatetimezone (timezone newzone). However, considering that the execution time of TimerTask is too short, only 1 second, it is basically no different from updating immediately. If TimerTask takes a long time to execute, it is necessary to update the displayarea at once.

Supplement:

①. Pack () is used to automatically calculate the screen size; ②. TimeZone. Getavailableids () is used to get all timezones.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>