Emulator's clock doesn't match the host system clock
Asked Answered
C

8

77

Why doesn't the Android emulator's clock match the host system clock? It's not a time zone difference--it's always off by several minutes.

Is there a way to synchronize them besides manually setting the emulator's time?

Chinchin answered 21/6, 2012 at 23:33 Comment(6)
Can you set the time via the settings the same way you can on a device?Carpathoukraine
@Vestibule : Urrrm, if I understand the OP correctly, I think he means the 'Clock' as in the thing which shows the current time on the screen.Emendate
Sorry, I don't have enough reputation yet to post a comment on the asker's question, or else I would. When you launch your emulator through the Android AVD Manager, do you have save to snapshot or launch from snapshot enabled?Barm
@Emendate Seems these "Clock things" are confusing, here's a screenshot with the emulator's UTC (Universal Time) and my host system's time.Vestibule
@Vestibule : I think you missed my point. The OP states "it's often off by several minutes." which clearly isn't a time-zone variation from UTC. You might create AVDs which default to UTC but I set mine to my own locale which means the AVD shows my time-zone and not UTC.Emendate
Which operating system are you using? I had exactly the same issue and I fixed it via a script I installed in my crontab. This can be easily done in MacOS (as I did) or in Linux systems.Nadabus
A
65

I had the same problem and got it resolved by just restarting the emulator using emulator power button.

Abingdon answered 5/3, 2020 at 2:19 Comment(4)
I needed a Cold Boot from AVD manager but then this worked for meAglow
you can use command adb rebootHeadwaters
I used " wipe data " using AVD manager and then did a " Cold Boot Now " and it worked!Pun
Thanks @Balaramakrishna Rachumallu, Working for me after restart emulatorArnaldo
C
24

I believe there is no way to synchronize the time. The default image of the emulator sets to UTC/GMT (+00:00). However you can change it to your own.

Here is an image on how to do so: First un-check the "Automatic Time Zone" (red arrow) then click on the "Selected Time Zone" (green arrow) and finally select your time zone and it should match the one on your system (yellow arrow). timezone screen shots

Clarkin answered 22/6, 2012 at 0:5 Comment(6)
even if the minutes are off on his setup as he seems to indicate, it should still be possible to fix it this way by taking off automatic date&time and manually setting both hours and minutes to whatever you please. Including whatever the system time is.Carpathoukraine
yes :) as you mentioned it would be possible to "synchronize" it by setting it manually.Clarkin
I stated in the question that this is not a timezone problem, that it is off by several minutes (not hours).Chinchin
This worked for me. Although I admit, my issue was not exactly the same--just a timezone problem.Adenectomy
@JeffAxelrod: Apparently, your question got a life of its own, independent of your original intentions: People like me, who do have a timezone problem, get your question as the top Google result, get helped by the first answer and upvote it. I am tempted to modify your question to match the answers, but I would not want to do that without your consent.Lundberg
@Lundberg LOL, no, I wasn't asking about how to synchronize time zones. So much so that I bolded the question to indicate that specifically.Chinchin
C
12

On AndroidTV API 28 simulator, this works (GNU/Linux, and some Mac) :

adb shell su root date $(date +%m%d%H%M%Y.%S)

The host timezone should match your AndroidTV timezone.

Chism answered 13/1, 2020 at 7:55 Comment(2)
worked on my Mac m1Masson
There's a fuller answer on how to do this here: https://mcmap.net/q/266530/-emulated-android-device-does-not-re-sync-time-date-after-restoring-snapshotWindshield
P
10

The date command has changed in newer androids since the other answers. You must have root, -s is not recognized and the set format has changed.

I've had luck using adb shell su root date -u @$(date +%s.%N), which uses @ to set it using UNIX timestamp seconds with nanosecond precision. -u is required if you have changed the timezone for some reason, even though timestamps should not have a time zone!

Physician answered 26/10, 2017 at 2:46 Comment(1)
Thank you. This is the only answer that has worked in recent times. For macOS, just skip the nanoseconds, so: adb shell su root date -u @$(date +%s)Starnes
U
9

If there are some discrepancies between the AVD and host times, mainly after restarting from a snapshot you can use adb shell date to check and/or set the date just after you launched the emulator.

Uis answered 22/6, 2012 at 1:3 Comment(2)
adb shell date is a effective way though it can not solve the issue completelyUndermost
date: cannot set date: Operation not permitted :(. emulator version 34.1.20.0, using API 34 arm64-v8a google play image.Shrink
T
5

As I've got this as top link in google :)

On windows you can get right time by issuing

adb shell date -s %date:~10,4%%date:~4,2%%date:~7,2%.%time:~0,2%%time:~3,2%%time:~6,2%

-s value should be in format: YYYYMMDD.hhmmss

On systems that have different time format, portion with windows shell args will be different!

You can test out in cmd.exe:

> echo %date%
Thu 01/22/2015

To output YYYY part use: %date:[position of char. 0-based],[length]%

On linux:

adb shell date -s $(date +%Y%m%d.%H%M%S)
Tease answered 22/1, 2015 at 21:42 Comment(3)
on mac terminal: works only on emulator or rooted device. adb shell su root "date $(date +%m%d%H%S)"Crossroads
@Crossroads on macOS your command gives me error /system/bin/sh: su: not foundMultifoliate
@Crossroads has a slight mistake, it should be: adb shell su root "date $(date +%m%d%H%M)"Nystatin
C
1

On Windows, you need to put quote around the datetime string to avoid error when the parsing returns a space in front of a digit.

The complete syntax would be:

adb shell date -s '%date:~10,4%%date:~4,2%%date:~7,2%.%time:~0,2%%time:~3,2%%time:~6,2%'
Colure answered 16/5, 2015 at 11:12 Comment(0)
L
1

Mackbook Air M2 - Android Studio Flamingo-emulator API 33.Had the same issue.Cold reboot after setting time zone of emulator worked for me.

Luganda answered 15/6, 2023 at 7:1 Comment(1)
Linux (Fedora) had the same issue and solved by the same method.Unthrone

© 2022 - 2024 — McMap. All rights reserved.