gpt4 book ai didi

java - jodatime-PeriodFormatter : suffix only for day/days

转载 作者:行者123 更新时间:2023-11-30 10:30:02 30 4
gpt4 key购买 nike

我只需要显示天/天的后缀,我该如何实现?它不起作用:

java.lang.IllegalStateException: No field to apply suffix to..

    private PeriodFormatter getDayTextFormatter() {
return new PeriodFormatterBuilder()
.printZeroNever()
.appendSuffix("day", "days")
.toFormatter();
}

最佳答案

我认为这是不可能的。根据JodaTime's javadoc , appendSuffix 方法将在没有字段附加后缀时抛出异常:

Throws: IllegalStateException - if no field exists to append to

所以我相信这次JodaTime帮不了你了。虽然,您可以这样做:

private String suffix(Period p) {
int days = p.getDays();
if (days <= 0) {
return "";
}

return days == 1 ? "day" : "days";
}

使用此代码,以下内容:

System.out.println(suffix(Period.days(1)));
System.out.println(suffix(Period.days(2)));
System.out.println(suffix(new Period()));

产生输出:

day
days
// and a line with an empty string

关于java - jodatime-PeriodFormatter : suffix only for day/days,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43766748/

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