Monday, January 21, 2008

Setting emulator time in Android

Setting the emulator time in android is fairly easy using the date command inside the android emulator image. If you are developing and testing a time related application you may found useful to change system time to force some events to happen, like an appointment alarm.

To do this, just use

$ adb shell date secs

where secs is seconds since 1970-01-01 00:00:00 UTC.

Emulator time is updated every minute, so you have to wait until the display is update, however system time has already been changed.

If you are in Linux and want to obtain secs for an arbitrary date

$ date --date="2008-01-31 17:46:59" +%s

or as a one-liner

$ adb shell date $(date --date="2008-01-31 17:46:59" +%s)

notice that we are using two different date commands, one from the android platform and the other from our Linux system. If you are using Windows or OSX, I'm sure you can find a similar way of doing this.

There are other alternatives, like installing Busybox in the system image, but this is left for another article. Stay tuned.
Post a Comment