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.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
This will be easier with Java 9. A
Duration
still isn’t formattable (what I know of), but methods for getting the hours, minutes and seconds are added, which makes the task somewhat more straightforward:Here is one more sample how to format duration. Note that this sample shows both positive and negative duration as positive duration.
This is a working option.
Call the method with
Produces something like:
In Scala, building up on YourBestBet's solution but simplified:
How about the following function, which returns either +H:MM:SS or +H:MM:SS.sss