Set date/time using ADB shell

2019-01-14 03:56发布

问题:

I´m trying to set the date/time using the ADB shell but the shell only returns the current time.

I´ve tried:

adb shell date -s YYYYMMDD.HHmmss

and unix time like:

adb shell date 1318349236

any ideas?

回答1:

To save storage space Android like many other embedded systems uses multi-call binaries to implement its basic command line tools like date.

Android device may include either toolbox or toybox (or both) binary depending on the version. You can check which implementation of the date tool available on your device by running toolbox date and toybox date commands. Then you can use the one which prints out the current date. For example for an Android 6.0+ device it might look like:

$ adb shell toybox date
Mon Jul 31 21:09:28 CDT 2017

$ adb shell toolbox date
date: no such tool

To set date and time using toolbox date use YYYYMMDD.HHmmss format:

adb shell "su 0 toolbox date -s 20161231.235959"

In case of toybox date use MMDDhhmm[[CC]YY][.ss] format:

adb shell "su 0 toybox date 123123592016.59"


回答2:

Android 6.0 has a new date format:

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.

And setting with -s no longer works. This is the updated set command:

Example of the updated command:

(while inside an adb shell)

date 060910002016.00                              

will result in:

Thu Jun  9 10:00:00 GMT 2016

Notice: The command will not be visible immediately on the device because it doesn't trigger any time change broadcast, But it will be visible within a minute.

To work around that, you can append this broadcast manually this way:

date 060910002016.00 ; am broadcast -a android.intent.action.TIME_SET

To call this with an adb command:

adb shell 'date 060910002016.00 ; am broadcast -a android.intent.action.TIME_SET'


回答3:

adb shell date -s `date +%G%m%d.%H%M%S`

At first it gets the current date of your machine and set it on the android target. This approach should work on Linux/Mac/Cygwin.



回答4:

You have to put date value if you want to change it. "-s" changes SET format. Default SET format is "MMDDhhmm[[CC]YY][.ss]".

I successfully tested the following commands:

adb root
adb shell "date `date +%m%d%H%M%Y.%S`"


回答5:

To make this work on my Xperia S, I had to break the commands as follows:

> adb shell
# su -
# date /* see current date */
# date -s YYYYmmdd

Motive: my device had reverted to the beginning of Linux time, and I wasn't particularly worried with time, all I wanted was to set the correct date -- which, BTW I couldn't do through system settings because my custom MIUI ROM kept crashing...



回答6:

Expanding on @uval's answer, you can use the following to update the date and time on the android device based on the time on your Windows machine:

set dateYYYY=%date:~10,4%
set dateMM=%date:~4,2%
set dateDD=%date:~7,2%
set timeHH=%time:~0,2%
set timeMM=%time:~3,2%
set timeSS=%time:~6,2%

adb shell su -c date %dateMM%%dateDD%%timeHH%%timeMM%%dateYYYY%.%timeSS%
adb shell su -c am broadcast -a android.intent.action.TIME_SET

Tested on a rooted Android 6.0 device and a Windows 10 PC.



回答7:

On some devices like RTAndroid maybe it works too:

adb shell "su 0 date `date +%m%d%H%M%Y.%S`"


回答8:

The correct format that has worked for me is yyyyMMddHHmm.ss



标签: android adb