这是我的句号格式化程序
PeriodFormatter humanFormat = new PeriodFormatterBuilder()
.appendPrefix("about ")
.appendHours()
.appendSuffix(" hrs")
.toFormatter()
.withLocale(Locale.getDefault());但是当我传递一个像5小时59分钟这样的时间段时,这个格式化程序会打印“大约5小时”,有没有办法将这个时间段绕到最接近的小时(5:59 ->大约6小时,5:29 ->大约5小时)?
发布于 2015-04-01 19:17:23
在PeriodFormatter中没有舍入小数的方法。
我可以建议你创建单独的方法,将Period转换为String,例如
static String PERIOD_FORMAT = "about %s hrs";
static String formatPeriod(Period p)
{
int hours = p.getHours();
hours = p.getMinutes() < 30 ? hours : hours + 1;
return String.format(PERIOD_FORMAT, hours);
//or 'return "about " + hours + " hrs"' - this solution is faster
}https://stackoverflow.com/questions/29386786
复制相似问题