How to format a duration in java? (e.g format H:MM:SS)

前端 未结 19 1498
情话喂你
情话喂你 2020-11-22 03:32

I\'d like to format a duration in seconds using a pattern like H:MM:SS. The current utilities in java are designed to format a time but not a duration.

19条回答
  •  抹茶落季
    2020-11-22 04:21

    If you don't want to drag in libraries, it's simple enough to do yourself using a Formatter, or related shortcut eg. given integer number of seconds s:

      String.format("%d:%02d:%02d", s / 3600, (s % 3600) / 60, (s % 60));
    

提交回复
热议问题