Parsing duration string into milliseconds

前端 未结 3 521
庸人自扰
庸人自扰 2021-01-02 15:22

I need to parse a duration string, of the form 98d 01h 23m 45s into milliseconds.

I was hoping there was an equivalent of SimpleDateFormat

3条回答
  •  时光取名叫无心
    2021-01-02 15:58

    The new java.time.Duration class in Java 8 let's you parse durations out of the box:

    Duration.parse("P98DT01H23M45S").toMillis();
    

    the format is slightly different so would need adjusting prior to parsing.

提交回复
热议问题