当 x 超过 3600 秒时,有没有办法将 'x' 秒转换为 y 小时和 z 秒?同样,当x超过60但小于3600秒时,使用JodaTime?将其转换为'a分b秒'。我知道我必须在 periodFormatter 中指定我需要的内容,但我不想指定它 - 我想要一个基于秒值的格式化文本。
这类似于您在论坛上发帖的方式,然后您的帖子最初会显示为“10 秒前发布”。1 分钟后,您会看到“1 分钟 20 秒前发布”,同样的情况持续数周、数天、数年。
我不知道为什么你不想指定你需要什么PeriodFormatter
. JodaTime
不知道如何将句点显示为字符串,因此您需要通过PeriodFormatter
.
由于 3600 秒等于 1 小时,正确使用格式化程序将自动为您完成此操作。下面是一个代码示例,在同一格式化程序上使用多个不同的输入,应该可以实现您想要的结果。
Seconds s1 = Seconds.seconds(3601);
Seconds s2 = Seconds.seconds(2000);
Seconds s3 = Seconds.seconds(898298);
Period p1 = new Period(s1);
Period p2 = new Period(s2);
Period p3 = new Period(s3);
PeriodFormatter dhm = new PeriodFormatterBuilder()
.appendDays()
.appendSuffix(" day", " days")
.appendSeparator(" and ")
.appendHours()
.appendSuffix(" hour", " hours")
.appendSeparator(" and ")
.appendMinutes()
.appendSuffix(" minute", " minutes")
.appendSeparator(" and ")
.appendSeconds()
.appendSuffix(" second", " seconds")
.toFormatter();
System.out.println(dhm.print(p1.normalizedStandard()));
System.out.println(dhm.print(p2.normalizedStandard()));
System.out.println(dhm.print(p3.normalizedStandard()));
产生输出::
1小时1秒
33分20秒
3天9小时31分38秒
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)