29

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?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Shimakaze
  • 291
  • 3
  • 4
  • For anyone who is looking for an extremely quick temporary and messy solution. Quit your emulator instance and start it back up with automatic time and time zone off. – Joseph Casey Apr 02 '17 at 06:40

6 Answers6

43

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

Linux

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"`

macOS

adb -e shell su root date -s `date +"%m%d%H%M%y"`

Windows

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

David Fraser
  • 6,475
  • 1
  • 40
  • 56
Artyom
  • 1,599
  • 12
  • 22
  • Just an addition to this good answer: if you use the `cd` command inside Windows PowerShell to get to the `adb` location, you may need to use `.\adb` instead of `adb` to execute commands. – Ziad Akiki May 07 '15 at 15:44
  • 5
    This is the command that works for me on MacOS: adb -e shell su root date \`date +"%m%d%H%M%y"\` – user836003 Dec 09 '16 at 20:27
  • `adb shell date -s` not work in MacOS. And @user836003 soution `adb -e shell su root date `date +"%m%d%H%M%y"`` works fine – Ninja Jan 12 '17 at 07:27
  • @user836003 solution worked for me on MacOS Sierra 10.12.2 but the example in the answer above did not work for me. – Carl Poole Mar 17 '17 at 15:29
  • in genymotion (or in newer android?) there's a new date format, so this worked for me: adb shell su root date $(date +"%m%d%H%M%Y.%S") – Gavriel Mar 06 '19 at 21:26
  • I've adapted this to use a date format that works. Using %Y%m%d.%H%M%S actually matches the POSIX format and sets the date wrongly in my experience on recent Android and Windows – David Fraser Apr 24 '23 at 07:50
4

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.

Peterdk
  • 15,625
  • 20
  • 101
  • 140
  • I would clarify your bug report with "EXPECTED RESULTS: It should resync the time on loading from snapshot [only when its preference setting tells it to automatically sync time/date from the network]." – Stephan Branczyk Jun 29 '13 at 07:51
4

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)

Jon
  • 9,156
  • 9
  • 56
  • 73
  • 1
    I used this and made an "External Tool" in Android Studio so I could just run it. My OAuth tokens are time sensitive and I was getting times that where 12hours or more off. Thanks. – DoctorD Jun 08 '17 at 14:07
1

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)
1

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.

Shereef Marzouk
  • 3,282
  • 7
  • 41
  • 64
  • 1
    Yes, here is one too. If you use SSL in your app, it needs to have the right time. I got strange errors due to it... Should file a bug on this. – Peterdk Jan 07 '13 at 18:47
0

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();
    }
}
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
  • Looks great, but could you please explain how to use it? – Simon Oct 05 '20 at 13:05
  • @Simon It’s nothing but just running ‘adb shell date ’ using Java. – Jin Kwon Oct 06 '20 at 05:11
  • So you packaged this script and run it separately every time you need it? Or is there a way to run it from the app ? – Simon Oct 06 '20 at 09:07
  • @Simon You'd better invoke `$ adb shell date ` in your plain terminal. Above Java program is nothing but invoking the script in a plain terminal. – Jin Kwon Oct 06 '20 at 09:37
  • thanks for your response, but thus, what's the interest of this code? – Simon Oct 06 '20 at 13:42
  • @Simon I thought, *7 years ago*, that I can do the `adb`-shit regardless of underlying OS. Things and thoughts change. :) – Jin Kwon Oct 06 '20 at 14:35