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?
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?
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"
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'
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`"
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.
For Android 6+, To change DateTime on Emulator:
Step 1
adb root
Step 2
adb shell date MMDDhhmm[[CC]YY][.ss]
For Example to set date to Oct 29 02:01:55 2020
(or 10/29/2020 02:01:55AM
) we do this:
adb shell date 102902012020.55
If you don't understand date format, just look at this:
10129202301420205.556
*Year and Seconds are optional.
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...
On some devices like RTAndroid maybe it works too:
adb shell "su 0 date `date +%m%d%H%M%Y.%S`"
I didn't have any issue with Android 5, most answers I found without su in the command works.
Android 6 is the one that got my attention.
On Android 6, it didn't give me error, it just didn't set it right, it will set it back to what it was before the command. I also ran into /system/bin/sh: su: not found errors
when I tried this https://stackoverflow.com/a/43481982/3922705
and this https://stackoverflow.com/a/19497572/3922705
After all the try-and-error sessions, I figured it out.
This is the date format I use MMDDhhmm[[CC]YY][.ss]
adb shell date '0739010002017.00'
This command didn't work, initially, in order work, it didn't work by itself.
I used this 3 steps and it works for me.
First, adb shell settings put global auto_time 1
(Turn on Automatic date & time)
Second, adb shell date '0739010002017.00'
( set time you want )
Third, adb shell settings put global auto_time 0
(Turn off Automatic date & time)
Noted: For my specific project, I can not rely on the network-provided and GPS-provided time. That's why I have the third command. which turn off the feature.
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.
Using the other answers for the current date I get the following error:
date: bad date '041009502020.23'; Fri Apr 10 09:50:23 CET 2020 != Fri Apr 10 10:50:23 CEST 2020
Using the format @%s
(%s
being the UNIX timestamp) that the command accepts aswell:
date: bad date '041009532020.32'; Sun Apr 10 09:53:32 CET 2020 != Fri Apr 10 10:53:32 CEST 2020
Turns out this has something to do with timezones, specifically it seems to have to do with DST summertime (since when I replace 04
with 03
(the month) it works just fine).
The solution to this problem is to specify the -u
option like so:
# toybox date -u 041009502020.23
Fri Apr 10 09:50:23 UTC 2020
Depending on your timezone this will set the time to a slightly off value, but after you ran that command you can check with the date
command to see how many hours you are off and simply account for that in the set date command!
On AndroidTV API 28 simulator, this works (GNU/Linux, and some Mac) :
adb shell su root date $(date +%m%d%H%M%Y.%S)
The host timezone should match your AndroidTV timezone.
Copy from my answer at : https://stackoverflow.com/a/59712493/1532175
The correct format that has worked for me is yyyyMMddHHmm.ss