JAVA支持显示经过的时间,支持月日
我需要在申请表中显示经过的时间。
我目前使用此代码执行此操作:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该使用 Date 类,因为日期是时间的一瞬间,但您将其用作持续时间。请仔细阅读joda time 文档。然后您可以使用
Period
和PeriodFormatter
来完成您的任务。或者如果您不想使用该库,请编写类似的内容。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
andPeriodFormatter
for you task. Or write something similar if you don't want use the library.避免遗留日期时间类
永远不要使用遗留类
Date
、Calendar
、SimpleDateFormat
等。这些在几年前就被现代的 java.time 类在 JSR 310 中定义,并内置于 Java 8 及更高版本中。java.time
始终使用 java.time 类进行日期时间处理。
持续时间
要存储不附加到时间线的时间跨度(以通用 24 小时日、小时、分钟、秒和纳秒为单位),请使用
Duration
类。生成标准 ISO 8601 格式的文本。
解析这样的标准文本。
您也可以指定日期,但这些日期与日历无关。它们是 24 小时的通用块。例如,指定其中两天与指定四十八小时完全相同。
您会发现 Thread.sleep 等方法已更新为接受 Duration 对象作为参数。
Period
如果您希望表示年-月-日范围内的时间跨度,请使用
Period
类。org.thirden.extra.PeriodDuration
如果您仔细想想,将年-月-日与小时-分钟-秒-纳秒结合起来并没有什么意义。但如果您坚持走这条路,请参阅
PeriodDuration
ThreeTen-Extra 库。ScheduledExecutorService
顺便说一句,
Timer
&TimerTask
类现在已成为遗留类,如其 Javadoc 中所述。在 Java 5+ 中,我们使用了 Executors 框架来减轻我们处理线程的繁琐工作。将您的任务定义为
Runnable
或Callable
。然后将该类型的对象提交给 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.Generate a standard ISO 8601 formatted text.
Parse such standard text.
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.
You will find methods such as
Thread.sleep
have been updated to acceptDuration
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
orCallable
. Then submit an object of that type to anExecutorService
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.