Converting Timestamp string “HH:mm:ss” to Duration

前端 未结 3 1127
囚心锁ツ
囚心锁ツ 2021-01-20 03:28

Just like the title says, I\'ve been able to isolate from a text file a string containing a duration in the format of \"HH:mm:ss.\" How would I go about converting it to a d

3条回答
  •  遇见更好的自我
    2021-01-20 03:57

    I would use String.split() to accomplish this. That will split it into an array based on the regex/separators that you specify in the parameters. Something like this,

    String time = "08:30:00";
    String[] splitValues = time.split(":");
    double hour = splitValue[0];
    double minute = splitValue[1];
    double seconds = splitValue[2];
    

    You could then add up the values. You know that there are 60 minutes in an hour, and 60 seconds in a minute. You could convert these to hours by dividing by 60 and 3600, respectively. Then, it's just a simple matter of addition.

提交回复
热议问题