Reputation: 291
If I do a fresh boot on the emulated device, it gets the correct current time from the host OS; however, if I reload the device from a snapshot, it gets the time/date from the moment the snapshot was created (e.g. When I shut down the emulator). The time/date does not re-sync after any amount of time. The only way around it that I've found is to manually update the time after restoring from a snapshot.
The Android Virtual Device has default properties:
Target = Android 4.0.3 - API Level 15
CPU/ABI = ARM (armeabi-v7a)
SD Card = N/A
Snapshot = Enabled
Abstract LCD density = 240
Max VM application heap size = 48
Device RAM size = 512
I've tried the emulator on OS X Snow Leopard and Windows 7, both show the same problem. Is there any way to get the emulator to automatically sync time after restoring from snapshot?
Upvotes: 29
Views: 9797
Reputation: 1599
I have been running into the same problem, and there does not seem to be a standard way of doing this. However, an emulator's date and time can be updated using the date
command of the ADB shell, which can be used in conjunction with standard commands for displaying date and time on your OS to update the emulator date and time to the current date and time.
According to the android date
command's --help
output, the following formats are acceptable:
MMDDhhmm[[CC]YY][.ss]
(POSIX)YYYY-MM-DD [hh:mm[:ss]]
(ISO 8601)The ISO 8601 format is most standard, and it supports using a T
instead of a space (which can cause problems in shell parameter passing).
To set a date and time of the emulator, you need to execute the following command in your OS (the su root
is often necessary to get date setting permissions), where $date
matches one of the above formats:
adb shell su root date -s $date
Setting the emulator date and time to the current date and time is relatively straightforward from a UNIX-style shell, so the following command will work on Linux:
adb shell su root date -s `date +"%Y-%m-%dT%H:%M:%S"`
adb -e shell su root date -s `date +"%m%d%H%M%y"`
On Windows (which I am using), the easiest way to do it is through Windows PowerShell:
adb shell su root date -s $(get-date -format yyyy-MM-ddTHH:mm:ss)
In Command Prompt, it is a bit more tricky because there is no way to specify a custom format to display date and time. The best way I found to get it in locale-independent format is by using the command wmic os get LocalDateTime
(line 2). Its date-time format can be parsed to adapt to the format needed by the ADB shell: the symbols :~
can be used to print a substring of an environment variable contents, with the format %var:~<start-index>,<number-of-chars>%
. We also need to ignore everything except line 2, so the full command that you need to run is as follows:
for /f "skip=1 delims=" %A in ('wmic os get localDateTime') do @for /f "delims=" %B in ("%A") do @cmd /v /c "set wmicdate=%B & adb shell date -s !wmicdate:~0,4!-!wmicdate:~4,2!-!wmicdate:~6,2!T!wmicdate:~8,2!:!wmicdate:~10,2!:!wmicdate:~12,2!"
For the curious: this first saves the date-time into the %wmicdate%
variable and then passes it to ADB by parsing it appropriately. The !
are used instead of %
to read the variable on-the-fly. This is all done in a child cmd
process launched with the /v
option that enables this on-the-fly variable reading.
EDIT: Fixed the command for macOS (thanks @user836003).
Upvotes: 43
Reputation: 11
This one from macOS works best for me since it takes the time of the host to sync:
adb shell su root date -u @$(date -u +%s)
Upvotes: 1
Reputation: 9803
On a newer Android emulator running version 6 API 23, the following powershell command worked for me.
Windows Powershell
adb shell date $(get-date -format MMddHHmmyyyy.ss)
On Android emulator version 7 API 24:
adb shell su root date $(get-date -format MMddHHmmyyyy.ss)
Upvotes: 6
Reputation: 21997
Voted Arthon's answer up.
It seems that the emulator get loose to sync when the host machine get sleep.
I'm, personally, using following program for this.
public class AdbShellDateNow {
public static void main(final String[] args)
throws java.io.IOException, InterruptedException {
final long now = System.currentTimeMillis() / 1000L;
final ProcessBuilder builder =
new ProcessBuilder("adb", "shell", "date", Long.toString(now));
builder.redirectErrorStream(true);
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
final Process process = builder.start();
process.waitFor();
}
}
Upvotes: 0
Reputation: 16015
I opened a bug report.
I have the same kind of issues, and found out the hard way because my app that uses SSL, kept giving very weird errors. This was due to wrong date and time.
Apparently it's not yet reported.
Upvotes: 4
Reputation: 3392
I have searched many times before for a solution to this and i searched again when i saw your question but i couldn't find anyone else even complaining about this except you and me, maybe others don't create apps that time is critical or they test on a real device.
Conclusion: no there is not fix, you have to set it manually or not use snapshots.
Upvotes: 1