首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JodaTime PeriodFormatter,舍入以获得不精确的时间字符串(例如“大约3小时”)?

JodaTime PeriodFormatter,舍入以获得不精确的时间字符串(例如“大约3小时”)?
EN

Stack Overflow用户
提问于 2015-04-01 17:05:09
回答 1查看 196关注 0票数 0

这是我的句号格式化程序

代码语言:javascript
复制
PeriodFormatter humanFormat = new PeriodFormatterBuilder()
            .appendPrefix("about ")
            .appendHours()
            .appendSuffix(" hrs")
            .toFormatter()
            .withLocale(Locale.getDefault());

但是当我传递一个像5小时59分钟这样的时间段时,这个格式化程序会打印“大约5小时”,有没有办法将这个时间段绕到最接近的小时(5:59 ->大约6小时,5:29 ->大约5小时)?

EN

回答 1

Stack Overflow用户

发布于 2015-04-01 19:17:23

PeriodFormatter中没有舍入小数的方法。

我可以建议你创建单独的方法,将Period转换为String,例如

代码语言:javascript
复制
   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
   }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29386786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档