开发者

Set the date from a shell on Android

For debugging purposes, I would like to run my app on a certa开发者_开发技巧in date. I can change the date manually on my emulator, of course, but I was wondering if there was a simple way to do it from the emulator shell (I know it can't be done from the UI).


Passing seconds-since-epoch as per previous answer didn't work on my instance (WinXP Android SDK11 HVGA_1.6 emulator). However this particular format worked:

adb shell date -s "yyyymmdd.[[[hh]mm]ss]"

[Edited 6/27/2011: Added the time portion of the syntax.]


I am working with Win 7.

The correct format that worked for me is:

adb shell date -s "YYYYMMDD.hhmmss"

I saw in other threads this command, what really made the difference for me was to set under settings->Date & Time the options in this way. (Unselect the automatic date and time and time zone)

Set the date from a shell on Android

Set the date from a shell on Android


This works for me under Linux, perhaps some correction for daylight savings will be required:

adb shell date -s $(date +%Y%m%d.%H%M%S)


Latest instructions, as of September 2022, for Android 9 and above:

Please see this answer: Emulated Android device does not re-sync time/date after restoring snapshot


Old instructions from April 2017, which don't work for Android 9/10/11/12/13 and above:

The following works on Mac OS X to align exactly with your computer's time, and it sets the seconds as well.

For Android 6/7/8 emulators, use this:

adb -e shell date $(date +%m%d%H%M%Y.%S)

For Android 4/5 emulators, use this:

adb -e shell date -s $(date "+%Y%m%d.%H%M%S")

For example, you could use a shell script to detect which Android OS version is running and run the correct adb command appropriately:

#!/bin/bash
android_os_version=$(adb -e shell getprop ro.build.version.sdk)
# The adb function will return a string like "23\n". So you must remove the new line
# character from it, otherwise the integer comparison (below) will not work.
# See https://stackoverflow.com/questions/19505227/integer-expression-expected-error-in-shell-script/#48705290
android_os_version="${android_os_version//[$'\t\r\n ']}"

echo "The emulator has android_os_version = $android_os_version"
if [ "$android_os_version" -gt 22 ] ; then
    echo "Android 6.0+ emulator detected."
    adb -e shell date $(date +%m%d%H%M%Y.%S)
else
    echo "Android 2/4/5 emulator detected."
    adb -e shell date -s $(date "+%Y%m%d.%H%M%S")
fi

Note 1: -e means "emulator", and it works reliably when you have both an emulator and device connected at the same time.

Note 2: The first variation was found by following on from the comment made by Siva Kranthi Kumar, on 9 June 2016. Attempting to use the documentation from adb shell date -? is virtually impossible. It's poorly written and far too difficult to understand.


Unlike linux, there are only 2 ways you can set date. date command will set time by calling device driver 'dev/alarm'.

  1. date -s "YYYYMMDD.hhmmss" or
  2. date -u "UTC_TIME_MILLI_SECS"

Hope it helps.

Ravi Pandit


This works on a mac:

adb shell date $(date -j mmddHHMM +%s)

where "mmddHHMM" is the month, day, hour, and minute of the time you want to set.

This will invoke the "date" command in your emulator by using adb shell, then pass it the number of seconds since epoch to set the time to (as calculated by calling "date -j..." on your local mac)

You can also be more specific. The format from the manpage for date (on a mac) is [[[mm]dd]HH]MM[[cc]yy][.ss]


Previous not work for me. Work fine on Android 11 from Ubuntu 20:
adb shell su root date @$(date +%s) set
Thu Jul 7 06:34:12 GMT 2022

Without "su" was date: cannot set date: Operation not permitted

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜