JAVA支持显示经过的时间,支持月日

发布于 2024-12-10 16:31:38 字数 626 浏览 1 评论 0原文

我需要在申请表中显示经过的时间。
我目前使用此代码执行此操作:

    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);

此代码显示小时、分钟和时间。秒正确,但我怎样才能显示天数和时间?几个月? new SimpleDateFormat("MD HH:mm:ss") 无法执行此操作,例如,当 5 秒过去时,它也会显示 1 个月又 1 天!

我怎样才能同时显示月份和日期?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅沫记忆 2024-12-17 16:31:38

您不应该使用 Date 类,因为日期是时间的一瞬间,但您将其用作持续时间。请仔细阅读joda time 文档。然后您可以使用 PeriodPeriodFormatter 来完成您的任务。或者如果您不想使用该库,请编写类似的内容。

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.

蝶舞 2024-12-17 16:31:38

避免遗留日期时间类

永远不要使用遗留类 DateCalendarSimpleDateFormat 等。这些在几年前就被现代的 java.time 类在 JSR 310 中定义,并内置于 Java 8 及更高版本中。

java.time

始终使用 java.time 类进行日期时间处理。

持续时间

要存储不附加到时间线的时间跨度(以通用 24 小时日、小时、分钟、秒和纳秒为单位),请使用 Duration 类。

Duration duration = Duration.ofHours( 5 ).plusMinutes( 4 ).plusSeconds( 3 );

生成标准 ISO 8601 格式的文本。

String iso8601Text = duration.toString() ;

解析这样的标准文本。

Duration duration = Duration.parse ( "PT5H4M3S" ) ;

您也可以指定日期,但这些日期与日历无关。它们是 24 小时的通用块。例如,指定其中两天与指定四十八小时完全相同。

Duration duration = Duration.ofDays( 2 ) ;  // 48 hours, not calendar days.
Duration theSame = Duration.ofHours( 48 ) ;  // 2 days.

您会发现 Thread.sleep 等方法已更新为接受 Duration 对象作为参数。

Period

如果您希望表示年-月-日范围内的时间跨度,请使用 Period 类。

org.thirden.extra.PeriodDuration

如果您仔细想想,将年-月-日与小时-分钟-秒-纳秒结合起来并没有什么意义。但如果您坚持走这条路,请参阅 PeriodDuration ThreeTen-Extra 库。

ScheduledExecutorService

顺便说一句,Timer & TimerTask 类现在已成为遗留类,如其 Javadoc 中所述。

在 Java 5+ 中,我们使用了 Executors 框架来减轻我们处理线程的繁琐工作。将您的任务定义为 RunnableCallable。然后将该类型的对象提交给 ExecutorService 来运行。

特别是,要安排任务在延迟后运行,请使用 ScheduledExecutorService。您甚至可以要求该界面在指定的时间间隔内重复运行任务。

Avoid legacy date-time classes

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.

java.time

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文