有没有简单的方法可以将时间间隔(年龄)转换为文本? 例如 - 年龄为 25.45 岁。我需要转换为“25 年 3 个月 1 天”。问题不是关于数字 25、3、1,而是如何使用正确形式(复数、变格)将年/月/日翻译成不同的语言。英语似乎很容易硬编码,但其他语言则不然,我更喜欢一些通用的解决方案。
数字/英语/捷克语/...
1/天/书房
2/天/dny
5/天/dnů
...
最佳答案
Joda time 可以很容易地做到这一点。
例如:
public static void main(String[] args) {
PeriodFormatter daysHoursMinutes = new PeriodFormatterBuilder()
.appendDays()
.appendSuffix(" day", " days")
.appendSeparator(" and ")
.appendMinutes()
.appendSuffix(" minute", " minutes")
.appendSeparator(" and ")
.appendSeconds()
.appendSuffix(" second", " seconds")
.toFormatter();
Period period = new Period(72, 24, 12, 0);
System.out.println(daysHoursMinutes.print(period));
System.out.println(daysHoursMinutes.print(period.normalizedStandard()));
}
将打印:
24 minutes and 12 seconds
3 days and 24 minutes and 12 seconds
另请参阅:Period to string
关于java - 将时间间隔转换为不同语言的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14693195/