Convert the string "01:03" to seconds

Scenario: We often encounter this kind of scenario. Users use front-end time components to customize the length of time, such as antd's timePicker, and pass the selected time to the backend. However, in the backend, we usually use seconds or minutes to save , without saving a string.
At this time, we need to replace "01:03" (hour and minute) with seconds.
TimePicker

"01:03" can be converted to seconds using the following code:

String timeString = "01:03";
String[] timeParts = timeString.split(":");
int minutes = Integer.parseInt(timeParts[0]) * 60 * 60 + Integer.parseInt(timeParts[1]) * 60;
System.out.println("秒数:" + minutes);

This code splits the input time string into hours and minutes by a colon, then multiplies the hours by 60 and adds the minutes to get the total number of minutes. Then multiply the total minutes by 60 to get the total seconds.

Supongo que te gusta

Origin blog.csdn.net/qq_43720551/article/details/131386618
Recomendado
Clasificación