convert joda-time seconds to formatted time string

前端 未结 3 1898
伪装坚强ぢ
伪装坚强ぢ 2021-01-18 09:46

say I have 110 seconds that I want to convert to 01:50 or 00:01:50 or such. How do I do that in joda-time? I load the number into Seconds but then

3条回答
  •  清酒与你
    2021-01-18 10:04

    LocalTime time = new LocalTime(0, 0); // midnight
    time = time.plusSeconds(110);
    String output = DateTimeFormat.forPattern("HH:mm:ss").print(time);
    System.out.println(output); // 00:01:50
    

    Note this answer is valid for amounts of seconds less than one full day (86400). If you have bigger numbers then better use a formatter for durations (in Joda-Time called PeriodFormatter) - see also the right answer of @Isaksson.

提交回复
热议问题