测试环境: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
(月日时分年.秒)
例如:date 052514192019.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 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) {
}
}
}

调用示例

String curr_time = “052514412019.52”;
execSuCmd("date " + curr_time
+ "\n busybox hwclock -w\n");

参考:https://blog.csdn.net/q1075355798/article/details/84660423

Android 使用date set命令修改系统时间的更多相关文章

  1. Linux 命令修改系统时间

    修改linux的系统时间使用date指令,date命令的功能是显示和设置系统日期和时间. 输入date 查看目前系统时间. 修改时间需要 date -功能字符 修改内容 命令中各选项的含义分别为:-d ...

  2. linux修改系统时间date命令加clock -w

    http://m.jb51.net/LINUXjishu/117784.html 修改linux系统时间的方法(date命令) 11-18 23:22:27作者:脚本之家 命令格式为: date -s ...

  3. Centos-显示或修改系统时间与日期-date

    date 显示或者修改系统时间与日期,只有超级用户才能用date命令设置和修改时间,普通用户只能显示时间 相关参数 -s 设置设置时间,格式为 Y-m-d H:M:S -d    对日期进行运算, + ...

  4. Linux下修改系统时间并写入BIOS

    我们一般使用“date -s”命令来修改系统时间.比如将系统时间设定成2005年7月26日的命令如下. #date -s 07/26/2005 将系统时间设定成下午11点12分0秒的命令如下. #da ...

  5. Linux怎样修改系统时间

    修改linux的时间可以使用date指令 修改日期: 时间设定成2009年5月10日的命令如下: #date -s 05/10/2009 修改时间: 将系统时间设定成上午10点18分0秒的命令如下. ...

  6. CentOS修改系统时间

    CentOS修改系统时间 操作: 1. date –s '1987-05-02 10:10:10' 2. clock –w //将日期写入CMOS 补充: 修改Linux时间一般涉及到3个命令: 1. ...

  7. Linux命令-更新系统时间和硬件时间

    查看系统时间和时区: date 查看系统时间date -R 查看时区 修改时区: tzselect 修改时区 或 cp /usr/share/zoneinfo/Asia/Shanghai /etc/l ...

  8. linux修改系统时间和时区

    1.修改系统时间linux系统时钟有两个,一个是硬件时钟,即BIOS时间,就是我们进行CMOS设置时看到的时间,另一个是系统时钟,是linux系统Kernel时间.当Linux启动时,系统Kernel ...

  9. linux 修改系统时间 同步网络时间

    一.date命令 date -s time  修改系统时钟时间为time 设置时间和日期 例如:将系统日期设定成2018年6月8日的命令 命令 : "date -s 06/08/2018&q ...

随机推荐

  1. vbox的四种网络模式

      一.NAT模式 特点: 1.如果主机可以上网,虚拟机可以上网 2.虚拟机之间不能ping通 3.虚拟机可以ping通主机(此时ping虚拟机的网关,即是ping主机) 4.主机不能ping通虚拟机 ...

  2. Odoo的 base 模型

    Odoo 内核中有一个base插件模块.它提供了 Odoo 应用所需的基本功能.然后有一组内置插件模块来提供标准产品中的官方应用和功能.base模块中包含两类模型: 信息仓库(Information ...

  3. Kernel boot options

    There are three ways to pass options to the kernel and thus control its behavior: When building the ...

  4. 《python解释器源码剖析》第3章--python中的str对象

    3.0 序 我们知道python中的字符串属于变长对象,当然和int也是一样,底层的结构体实例所维护的数据的长度,在对象没有定义的时候是不知道的.当然如果是python2的话,底层PyIntObjec ...

  5. 05-【session、cookie】

    session.cookie 1.HttpSession概述>HttpSession是由JavaWeb提供的,用来会话跟踪的类.session是服务器端对象,保存在服务器端!!!>Http ...

  6. 解决PHP中json_encode()不支持中文的替代函数

    json_encode()是PHP中将数组转换成JSON格式的函数,因为各种原因只要是中文就会乱码= = 找到了这么一个函数替代了原有的json_encode(),能很好地输出中文~ function ...

  7. 开源框架相关面试问题-butterknife注解框架面试问题讲解

    butterknife使用简介: 它的出现主要是为了解决咱们在android开发中会写大量的findViewById().setOnClickListener()这样的索然无味的代码,其实它就是一个依 ...

  8. c++分布式服务框架teamtalk

    这是蘑菇街开发的内部通讯软件,记录一下.可以参考学习 https://github.com/meili/TeamTalk

  9. 布隆算法(BloomFilter)

          BloomFilter算法,是一种大数据排重算法.在一个数据量很大的集合里,能准确断定一个对象不在集合里:判断一个对象有可能在集合里,而且占用的空间不大.它不适合那种要求准确率很高的情况, ...

  10. flutter 记录正则匹配

    手机号正则匹配: // 正则匹配 static bool isChinaPhoneLegal(String str) { return new RegExp('^((13[0-9])|(15[^4]) ...