java 設置window系統時間和設置Linux系統時間

public static String setDatetime(String date, String time) {
    String osName = System.getProperty("os.name");
    String dateTimeMessage = date + " " + time;
    try {
        if (osName.matches("^(?i)Windows.*$")) { // Window 系統
            String cmd;

            cmd = " cmd /c date " + date; // 格式:yyyy-MM-dd
            Runtime.getRuntime().exec(cmd);

            cmd = " cmd /c time " + time; // 格式 HH:mm:ss
            Runtime.getRuntime().exec(cmd);
        } else if (osName.matches("^(?i)Linux.*$")) {// Linux 系統
            String command = "date -s " + "\"" + date + " " + time + "\"";// 格式:yyyy-MM-dd HH:mm:ss
            Runtime.getRuntime().exec(command);
        } else {

        }
    } catch (IOException e) {
        return e.getMessage();
    }
    return dateTimeMessage;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章