“漂亮打印” java中的持续时间 [英] "pretty print" duration in java

查看:93
本文介绍了“漂亮打印” java中的持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道一个可以以几秒钟的速度打印数字的java库,就像c#一样吗?



例如。 123456 ms作为长时间打印为4d1h3m5s



(可能不准确,但您希望看到我会得到什么!!)



-ace

解决方案

Joda时间有一个很好的方法,使用 PeriodFormatterBuilder



Quick Win: PeriodFormat.getDefault()。print (duration.toPeriod());



例如

 code> // import org.joda.time.format.PeriodFormatter; 
// import org.joda.time.format.PeriodFormatterBuilder;
// import org.joda.time.Duration;

持续时间=新的持续时间(123456); // in milliseconds
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendDays()
.appendSuffix(d)
.appendHours()
.appendSuffix( h)
.appendMinutes()
.appendSuffix(m)
.appendSeconds()
.appendSuffix(s)
.toFormatter();
String formatted = formatter.print(duration.toPeriod());
System.out.println(格式化);


Does anyone know of a java library that can pretty print a number in milli seconds in the same way that c# does?

E.g. 123456 ms as a long would be printed as 4d1h3m5s

(might not be accurate , but you see hopefully what I'm getting at!!)

-ace

解决方案

Joda Time has a pretty good way to do this using a PeriodFormatterBuilder.

Quick Win: PeriodFormat.getDefault().print(duration.toPeriod());

e.g.

//import org.joda.time.format.PeriodFormatter;
//import org.joda.time.format.PeriodFormatterBuilder;
//import org.joda.time.Duration;

Duration duration = new Duration(123456); // in milliseconds
PeriodFormatter formatter = new PeriodFormatterBuilder()
     .appendDays()
     .appendSuffix("d")
     .appendHours()
     .appendSuffix("h")
     .appendMinutes()
     .appendSuffix("m")
     .appendSeconds()
     .appendSuffix("s")
     .toFormatter();
String formatted = formatter.print(duration.toPeriod());
System.out.println(formatted);

这篇关于“漂亮打印” java中的持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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