44

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?

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Abraão Caldas
  • 645
  • 1
  • 7
  • 8
  • 2
    First one should work. Can you write actual command (with replaced YYYYMMDD.HHmmss values) you have sent to device? – Srđan Tot Oct 22 '13 at 21:19
  • Possible duplicate of [Cannot run adb shell "date \`date +%m%d%H%M%Y.%S\`"](https://stackoverflow.com/questions/38938791/cannot-run-adb-shell-date-date-mdhmy-s) – iegik Jun 26 '17 at 20:57

12 Answers12

57

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"
Alex P.
  • 30,437
  • 17
  • 118
  • 169
42

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'
Panda
  • 6,955
  • 6
  • 40
  • 55
Amir Uval
  • 14,425
  • 4
  • 50
  • 74
  • -u means to always use UTC. To set the time in the context of the current time zone, just use "date 060910002016.00" on Marshmallow. – Nova Entropy Aug 03 '16 at 09:03
  • 1
    @tristan2468 -- thank you! corrected (removed the unneeded -u) – Amir Uval Aug 03 '16 at 09:15
  • Thanks @uval, exactly what i needed! – Luis Sep 21 '16 at 16:54
  • 1
    I can confirm this works on Android Things devpreview 4.1 which is based on Nougat `date 081812242017.40` with root permission, otherwise error said `date: cannot set date: Operation not permitted Fri Aug 18 12:24:10 GMT 2017` – adadion Aug 18 '17 at 05:28
  • Sorry, but the new date format is somewhat weird, especially when the ISO format YYYY-MM-DD HH:MM:SS is ubiquitous. I'm wondering when they start to require seconds since the EPOCH... – roffez Nov 08 '17 at 09:02
  • @user3806649 in which scenario? This question is about ADB shell. If you can't find an answer on StackOverflow - open a dedicated question for it. – Amir Uval Dec 10 '17 at 15:50
  • /system/bin/sh: date 060910002016.00 ; am broadcast -a android.intent.action.TIME_SET: not found – Divyanshu Kushwaha Aug 10 '18 at 06:35
  • @DivyanshuKushwaha note the "(while inside an adb shell)" – Amir Uval Aug 10 '18 at 20:49
  • @auval Thanks, the command worked without commas i.e. `adb shell date 060910002016.00 ; am broadcast -a android.intent.action.TIME_SET` – Divyanshu Kushwaha Sep 06 '18 at 05:12
  • I also needed to `adb shell hwclock -w` to persist the system time to the rtc (android 8.1) – jtomson May 21 '19 at 19:32
33

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`"
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Ivan Kovtun
  • 645
  • 1
  • 11
  • 18
16
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.

Greg Rynkowski
  • 556
  • 6
  • 20
7

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

  1. Month MM = 10
  2. Day DD = 29
  3. Hour hh = 02
  4. Minute mm = 01
  5. Year [[CC]YY] = 2020 (or can be 20)
  6. Second [.ss] = 55

*Year and Seconds are optional.

Sabrina
  • 2,531
  • 1
  • 32
  • 30
3

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...

Midas
  • 31
  • 1
3

On some devices like RTAndroid maybe it works too:

adb shell "su 0 date `date +%m%d%H%M%Y.%S`"
Hpsaturn
  • 2,702
  • 31
  • 38
2

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.

NOT_A_PROGRAMMER
  • 1,794
  • 2
  • 20
  • 31
1

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.

0

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!

confetti
  • 1,062
  • 2
  • 12
  • 27
0

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

FredG
  • 712
  • 7
  • 10
-2

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

Ranhiru Jude Cooray
  • 19,542
  • 20
  • 83
  • 128