在Java 8 / jsr310中格式化持续时间 [英] Formatting a Duration in Java 8 / jsr310

查看:97
本文介绍了在Java 8 / jsr310中格式化持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将项目从Joda-Time过渡到java8的本地时间库,我遇到了麻烦。我一直无法找到持续时间的格式化程序。我希望有一个自定义字符串格式,例如,HHH + MM,其中75小时和15分钟的持续时间格式为75 + 15。通过转换为句点并使用PeriodFormatter,这很容易用Joda-Time,但我无法在Java8中找到这种类型的类。我错过了什么?

I am transitioning a project from Joda-Time to java8's native time libraries, and I have run into a snag. I have been unable to find a formatter for Duration. I would like to have a custom String format of, for instance, HHH+MM, where a Duration of 75 hours and 15 minutes would format as "75+15". This was easy to do with Joda-Time by converting to period, and using a PeriodFormatter, but I have been unable to find this type of class in Java8. Am I missing something?

推荐答案

jsr-310中没有句点/持续时间格式化程序,与JodaTime不同。并非JodaTime的每个功能都移植到JSR-310(例如也不是PeriodType)。反之,JSR-310有一些JodaTime中没有的功能(例如本地化的工作日数字或带调整器的策略模式方法)。

There is no period/duration-formatter in jsr-310, different from JodaTime. Not every feature of JodaTime was ported to JSR-310 (for example also not PeriodType). And in reverse JSR-310 has some features which are not available in JodaTime (for example localized weekday numbers or the strategy pattern approach with adjusters).

Java可能会发生这种情况9将介绍某种内置句点格式(从S. Colebourne读取一些内容)。

It might happen that Java 9 will introduce some kind of built-in period formatting (read something about this from S. Colebourne).

结论:JSR-310和JodaTime并不完全兼容另外,所以可能需要做很多工作。我不会那么热衷于移民。您是否需要JodaTime不提供的JSR-310的特殊功能?

Conclusion: JSR-310 and JodaTime are not fully compatible to each other, so a lot of work can be required. I would not be so keen on migration as soon as possible. Do you need special features of JSR-310 which are not offered by JodaTime?

附加说明:您还应该了解joda期间(包括所有单位)的事实从年到秒)与jsr310-period(仅年,月,日)或jsr310-持续时间(仅小时,分钟,秒和分数秒)不完全兼容。

Additional note: You should also be aware of the fact that joda period (which includes all units from years to seconds) is not fully compatible with jsr310-period (only years, months, days) or jsr310-duration (only hours, minutes, seconds and fraction seconds).

这篇关于在Java 8 / jsr310中格式化持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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