//字符串转秒数
public static Integer stringToTime(String value) {
if (StringUtils.isBlank(value)) {
return null;
}
boolean isMatch = Pattern.matches("^(?:[01]{0,1}\\d|2[0-3])(?::[0-5]{0,1}\\d){1,2}$", value);
if (!isMatch) {
return null;
}
Integer second = null;
String[] times = value.split(":");
if (times.length == 2) {
second = Integer.parseInt(times[0]) * 3600 + Integer.parseInt(times[1]) * 60;
}
if (times.length == 3) {
second = Integer.parseInt(times[0]) * 3600 + Integer.parseInt(times[1]) * 60 + Integer.parseInt(times[2]);
}
return second;
}
//秒转字符串
public String secondToTime(int second) {
if (second < 0) {
return "00:00:00";
} else {
String hour = String.format("%02d", second / 60 / 60);
String minute = String.format("%02d", second / 60 % 60);
String remainingSeconds = String.format("%02d", second % 60);
return hour + ":" + minute + ":" + remainingSeconds;
}
}