目录

工欲善其事

实践出真知

活跃标签: linux java mysql 待分类 js springboot win10 电路 vue macOS nginx esp32 git docker windows idea maven esp8266 python Arduino

存档:

Java 时间和秒互转

public class TimeUtil {

   
    public static void main(String[] args) {
        String minute = toMinute(3601);
        System.out.println("minute = " + minute);

        String timeStr = "1:00:01";
        int second = toSecond(timeStr);
        System.out.println("second = " + second);
    }

 	private static int toSecond(String timeStr) {
        if (timeStr != null) {
            String[] time = timeStr.split(":");

            int second = 0;
            for (int index = 0; index < time.length - 1; index++) {
                second += Integer.valueOf(time[index]) * (int) Math.pow(60, index);
            }
            return second + Integer.valueOf(time[time.length - 1]);
        }
        return 0;
    }
  
    public static String toMinute(int second) {
        if (second > 0) {
            int hours = second / 60 / 60;
            int minute = second / 60 % 60;
            int remainingSeconds = second % 60;
            return String.format("%02d:%02d:%02d", hours, minute, remainingSeconds);
        }
        return null;
    }

}

wsetbaseV0.0.1.zip


标题:Java 时间和秒互转
作者:llilei
地址:http://solo.llilei.work/articles/2023/11/09/1699525182430.html