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"
root
–
Jubilant adb shell "su 0 toybox date $(date +%m%d%H%M%Y.%S)"
works well for me –
Inflict 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'
date 081812242017.40
with root permission, otherwise error said date: cannot set date: Operation not permitted Fri Aug 18 12:24:10 GMT 2017
–
Harvin adb shell date 060910002016.00 ; am broadcast -a android.intent.action.TIME_SET
–
Noles adb shell hwclock -w
to persist the system time to the rtc (android 8.1) –
Besmear 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`"
alias adb-set-time='adb shell su 0 "date `date +%m%d%H%M%Y.%S`"'
–
Mutinous function adb-set-time { adb $@ shell su 0 "date `date +%m%d%H%M%Y.%S`"; }
. Usage: adb-set-time
or adb-set-time -e
etc –
Mutinous 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.
adb shell date -s "
date +'%G%m%d %H:%M:%S'"
–
Magness 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
Explanation of Date Format:
10129202301420205.556
*Year and Seconds are optional.
On some devices like RTAndroid maybe it works too:
adb shell "su 0 date `date +%m%d%H%M%Y.%S`"
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...
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://mcmap.net/q/349420/-set-date-time-using-adb-shell
and this https://mcmap.net/q/349420/-set-date-time-using-adb-shell
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://mcmap.net/q/265143/-emulator-39-s-clock-doesn-39-t-match-the-host-system-clock
can't add comment:
1, ISO 8601 format also works (see Supported input formats:
)
adb shell date '1970-01-17 14:08:09'
adb shell toybox date '1970-01-17 14:08:09'
# date --help
Toybox 0.8.4-android multicall binary: https://landley.net/toybox (see toybox --help)
usage: date [-u] [-I RES] [-r FILE] [-d DATE] [+DISPLAY_FORMAT] [-D SET_FORMAT] [SET]
Set/get the current date/time. With no SET shows the current date.
-d Show DATE instead of current time (convert date format)
-D +FORMAT for SET or -d (instead of MMDDhhmm[[CC]YY][.ss])
-I RES ISO 8601 with RESolution d=date/h=hours/m=minutes/s=seconds/n=ns
-r Use modification time of FILE instead of current date
-u Use UTC instead of current timezone
Supported input formats:
MMDDhhmm[[CC]YY][.ss] POSIX
@UNIXTIME[.FRACTION] seconds since midnight 1970-01-01
YYYY-MM-DD [hh:mm[:ss]] ISO 8601
hh:mm[:ss] 24-hour time today
All input formats can be followed by fractional seconds, and/or a UTC
offset such as -0800.
All input formats can be preceded by TZ="id" to set the input time zone
separately from the output time zone. Otherwise $TZ sets both.
+FORMAT specifies display format string using strftime(3) syntax:
%% 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)
%N nanosec (output only)
%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)
%F "%Y-%m-%d" %R "%H:%M" %T "%H:%M:%S" %z timezone (-0800)
%D "%m/%d/%y" %r "%I:%M:%S %p" %h "%b" %:z timezone (-08:00)
%x locale date %X locale time %c locale date/time %s unix epoch time
2, for another version date
adb shell toybox date -s '1970-01-17 14:08:09'
# date --help
Toybox 0.8.9-android multicall binary (see toybox --help)
usage: date [-u] [-I RES] [-r FILE] [-d DATE] [+DISPLAY_FORMAT] [-D SET_FORMAT] [SET]
Set/get the current date/time. With no SET shows the current date.
-d Show DATE instead of current time (convert date format)
-D +FORMAT for SET or -d (instead of MMDDhhmm[[CC]YY][.ss])
-I RES ISO 8601 with RESolution d=date/h=hours/m=minutes/s=seconds/n=ns
-r Use modification time of FILE instead of current date
-s DATE Set the system clock to DATE.
-u Use UTC instead of current timezone
Supported input formats:
MMDDhhmm[[CC]YY][.ss] POSIX
@UNIXTIME[.FRACTION] seconds since midnight 1970-01-01
YYYY-MM-DD [hh:mm[:ss]] ISO 8601
hh:mm[:ss] 24-hour time today
All input formats can be followed by fractional seconds, and/or a UTC
offset such as -0800.
All input formats can be preceded by TZ="id" to set the input time zone
separately from the output time zone. Otherwise $TZ sets both.
+FORMAT specifies display format string using strftime(3) syntax:
%% 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)
%N nanosec (output only)
%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)
%F "%Y-%m-%d" %R "%H:%M" %T "%H:%M:%S" %z timezone (-0800)
%D "%m/%d/%y" %r "%I:%M:%S %p" %h "%b" %:z timezone (-08:00)
%x locale date %X locale time %c locale date/time %s unix epoch time
The correct format that has worked for me is yyyyMMddHHmm.ss
© 2022 - 2024 — McMap. All rights reserved.