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; }
java 设置window系统时间和设置Linux系统时间
最新推荐文章于 2024-05-08 22:37:38 发布