大多数惯用的方法来打印Java中的时差? [英] Most idiomatic way to print a time difference in Java?

查看:146
本文介绍了大多数惯用的方法来打印Java中的时差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉打印时间差(毫秒):

I'm familiar with printing time difference in milliseconds:

 long time = System.currentTimeMillis();
 //do something that takes some time...
 long completedIn = System.currentTimeMillis() - time;

但是,是否有一种很好的方式以指定的格式打印完整的时间(例如:HH:MM :SS)使用Apache Commons甚至可怕的平台API的Date / Time对象?换句话说,在Java中从毫秒中写出时间格式最短,最简单,没有废话的方式是什么?

But, is there a nice way print a complete time in a specified format (eg: HH:MM:SS) either using Apache Commons or even the dreaded platform API's Date/Time objects? In other words, what is the shortest, simplest, no nonsense way to write a time format derived from milliseconds in Java?

推荐答案

Apache Commons具有 DurationFormatUtils 将指定格式应用于持续时间的类。所以,如下所示:

Apache Commons has the DurationFormatUtils class for applying a specified format to a time duration. So, something like:

long time = System.currentTimeMillis();
//do something that takes some time...
long completedIn = System.currentTimeMillis() - time;

DurationFormatUtils.formatDuration(completedIn, "HH:mm:ss:SS");

这篇关于大多数惯用的方法来打印Java中的时差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆