Skip to content Skip to sidebar Skip to footer

Set The Date From A Shell On Android

For debugging purposes, I would like to run my app on a certain date. I can change the date manually on my emulator, of course, but I was wondering if there was a simple way to do

Solution 1:

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

Solution 2:

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)

Date and time in the emulator

Commands from adb

Solution 3:

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)

Solution 4:

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 ] ; thenecho"Android 6.0+ emulator detected."
    adb -e shell date $(date +%m%d%H%M%Y.%S)
elseecho"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.

Solution 5:

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

Post a Comment for "Set The Date From A Shell On Android"