測試環境:android 7.1.1 java
在adb shell中試圖使用 date -s "yyyymmdd.[[[hh]mm]ss]"修改系統系統時間時,會提示 date: Unknown option sandroid
usage: date [-u] [-r FILE] [-d DATE] [+DISPLAY_FORMAT] [-D SET_FORMAT] [SET] Set/get the current date/time. With no SET shows the current date. Default SET format is "MMDDhhmm[[CC]YY][.ss]", that's (2 digits each) month, day, hour (0-23), and minute. Optionally century, year, and second. Also accepts "@UNIXTIME[.FRACTION]" as seconds since midnight Jan 1 1970. -d Show DATE instead of current time (convert date format) -D +FORMAT for SET or -d (instead of MMDDhhmm[[CC]YY][.ss]) -r Use modification time of FILE instead of current date -u Use UTC instead of current timezone +FORMAT specifies display format string using these escapes: %% literal % %n newline %t tab %S seconds (00-60) %M minute (00-59) %m month (01-12) %H hour (0-23) %I hour (01-12) %p AM/PM %y short year (00-99) %Y year %C century %a short weekday name %A weekday name %u day of week (1-7, 1=mon) %b short month name %B month name %Z timezone name %j day of year (001-366) %d day of month (01-31) %e day of month ( 1-31) %s seconds past the Epoch %U Week of year (0-53 start sunday) %W Week of year (0-53 start monday) %V Week of year (1-53 start monday, week < 4 days not part of this year) %D = "%m/%d/%y" %r = "%I : %M : %S %p" %T = "%H:%M:%S" %h = "%b" %x locale date %X locale time %c locale date/time date: Unknown option s
根據提示,使用如下命令git
命令格式:date MMddHHmmyyyy.ss set (月日時分年.秒) 例如:date 052514192019.22 set
date只是修改了系統時間,還應該把系統時間同步硬件時鐘,不然系統重啓後,時間是不會保存的shell
系統時間同步硬件時鐘,能夠用命令測試
busybox hwclock -w
busybox hwclock 語法以下this
usage: hwclock [-rswtluf] -f FILE Use specified device file instead of /dev/rtc (--rtc) -l Hardware clock uses localtime (--localtime) -r Show hardware clock time (--show) -s Set system time from hardware clock (--hctosys) -t Set the system time based on the current timezone (--systz) -u Hardware clock uses UTC (--utc) -w Set hardware clock from system time (--systohc)
經常使用的是spa
busybox hwclock -w --從系統時間設置到硬件時鐘
busybox hwclock -s --從硬件時鐘設置到系統時間
Android代碼(Android系統須要能獲取root權限).net
/** * 執行Android命令 * @param cmd 命令 */ private static void execSuCmd(String cmd) { Process process = null; DataOutputStream os = null; DataInputStream is = null; try { process = Runtime.getRuntime().exec("su"); os = new DataOutputStream(process.getOutputStream()); os.writeBytes(cmd + "\n"); os.writeBytes("exit\n"); os.flush(); int aa = process.waitFor(); is = new DataInputStream(process.getInputStream()); byte[] buffer = new byte[is.available()]; is.read(buffer); String out = new String(buffer); } catch (Exception e) { e.printStackTrace(); } finally { try { if (os != null) { os.close(); } if (is != null) { is.close(); } if (process != null){ process.destroy(); } } catch (Exception e) { } } }
調用示例code
String curr_time = 「052514412019.52」; execSuCmd("date " + curr_time + "\n busybox hwclock -w\n");
參考:https://blog.csdn.net/q1075355798/article/details/84660423orm