android+命令+修改时间,Android 使用date set命令修改系统时间

测试环境:android 7.1.1

在adb shell中试图使用 date -s "yyyymmdd.[[[hh]mm]ss]"修改系统系统时间时,会提示 date: Unknown option s

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

根据提示,使用以下命令

命令格式:date MMddHHmmyyyy.ss set(月日时分年.秒)

例如:date052514192019.22 set

date只是修改了系统时间,还应该把系统时间同步硬件时钟,否则系统重启后,时间是不会保存的

系统时间同步硬件时钟,可以用命令

busybox hwclock -w

busybox hwclock 语法如下

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)

常用的是

busybox hwclock -w --从系统时间设置到硬件时钟

busybox hwclock-s --从硬件时钟设置到系统时间

Android代码(Android系统需要能获取root权限)

/**

* 执行Android命令

* @param cmd 命令*/

private static voidexecSuCmd(String cmd) {

Process process= null;

DataOutputStream os= null;

DataInputStreamis = null;try{

process= Runtime.getRuntime().exec("su");

os= newDataOutputStream(process.getOutputStream());

os.writeBytes(cmd+ "\n");

os.writeBytes("exit\n");

os.flush();int aa =process.waitFor();is = newDataInputStream(process.getInputStream());byte[] buffer = new byte[is.available()];is.read(buffer);

Stringout = newString(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) {

}

}

}

调用示例

String curr_time = “052514412019.52”;

execSuCmd("date" +curr_time+ "\n busybox hwclock -w\n");

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值