gpt4 book ai didi

java - 将时间间隔转换为不同语言的文本?

转载 作者:搜寻专家 更新时间:2023-11-01 01:54:17 25 4
gpt4 key购买 nike

有没有简单的方法可以将时间间隔(年龄)转换为文本?例如 - 年龄为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com