简体   繁体   English

由 EL 以人类可读格式打印 Duration

[英]Print a Duration in human readable format by EL

First of all I'm new to java.time package.首先,我是java.time包的java.time

I'm writing a webapp that need to work with specific times of the day and with several durations of events.我正在编写一个需要处理一天中特定时间和多个事件持续时间的 web 应用程序。

So I wrote my code using LocalTime and Duration classes of java.time package.所以我写了用我的代码LocalTimeDuration的类java.time包。

When I need to render their value in JSP it is very simple for LocalTime object (because .toString() returns a human readable vale), so I can just write ${startTime} and everything goes in the right way (eg it is rendered as 9:00 ).当我需要在 JSP 中渲染它们的值时, LocalTime对象非常简单(因为.toString()返回一个人类可读的值),所以我可以只写${startTime}并且一切都以正确的方式进行(例如它被渲染如9:00 )。 The same approach doesn't work for Duration, because its representation is something like PT20M (in this case for 20 minutes).相同的方法不适用于 Duration,因为它的表示类似于PT20M (在本例中为 20 分钟)。

Does it exist an elegant way to perform a human-readable conversion in JSP directly by EL?是否存在一种优雅的方式来直接通过 EL 在 JSP 中执行人类可读的转换?

Yes, I know I can convert the object in a string in my classes (before JSP), but I'm searching for the suggested approach (that I'm not able to find)... another point is that I not see an official "convert()" (or whatever else) method in Duration object... so I'm thinking I'm using the wrong object to map a duration of time (to add or subtract on LocalTime s).是的,我知道我可以在我的类中转换字符串中的对象(在 JSP 之前),但我正在寻找建议的方法(我找不到)......另一点是我没有看到Duration 对象中的官方“convert()”(或其他任何方法)方法......所以我想我使用了错误的对象来映射持续时间(在LocalTime上添加或减去)。

Thank you.谢谢你。

Unfortunately there exists no elegant builtin way to format a Duration in Java 8. The best i have found is to use the method bobince describes in this answer:不幸的是,在 Java 8 中没有优雅的内置方式来格式化Duration 。我发现最好的方法是使用bobince这个答案中描述的方法:

    Duration duration = Duration.ofHours(1).plusMinutes(20);
    long s = duration.getSeconds();
    System.out.println(String.format("%d:%02d:%02d", s/3600, (s%3600)/60, (s%60)));

Which prints:哪个打印:

1:20:00 1:20:00

The code will have to be tuned if you need longer durations.如果您需要更长的持续时间,则必须调整代码。

I'm not sure what you mean that you are missing a convert method, but Duration is well suited for adding/subtracting on LocalTime .我不确定您缺少convert方法是什么意思,但Duration非常适合在LocalTime上添加/减去。 The methods LocalTime.plus() and LocalTime.minus() accepts Duration as argument. LocalTime.plus()LocalTime.minus()接受Duration作为参数。

If you're interested in words, apache commons will do the trick:如果你对文字感兴趣,apache commons 可以解决这个问题:

 DurationFormatUtils.formatDurationWords(System.currentTimeMillis() - start, true, false))
 2 days 1 hour 5 minutes 20 seconds

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/time/DurationFormatUtils.html#formatDurationWords-long-boolean-boolean- https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/time/DurationFormatUtils.html#formatDurationWords-long-boolean-boolean-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM