47

When you want to change the mobile system date or time in your application, how do you go about doing it?

6 Answers 6

70

You cannot on a normal off the shelf handset, because it's not possible to gain the SET_TIME permission. This permission has the protectionLevel of signatureOrSystem, so there's no way for a market app to change global system time (but perhaps with black vodoo magic I do not know yet).

You cannot use other approaches because this is prevented on a Linux level, (see the long answer below) - this is why all trials using terminals and SysExecs gonna fail.

If you CAN gain the permission either because you rooted your phone or built and signed your own platform image, read on.

Short Answer

It's possible and has been done. You need android.permission.SET_TIME. Afterward use the AlarmManager via Context.getSystemService(Context.ALARM_SERVICE) and its method setTime().

Snippet for setting the time to 2010/1/1 12:00:00 from an Activity or Service:

Calendar c = Calendar.getInstance();
c.set(2010, 1, 1, 12, 00, 00);
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
am.setTime(c.getTimeInMillis());

If you which to change the timezone, the approach should be very similar (see android.permission.SET_TIME_ZONE and setTimeZone)

Long Answer

As it has been pointed out in several threads, only the system user can change the system time. This is only half of the story. SystemClock.setCurrentTimeMillis() directly writes to /dev/alarm which is a device file owned by system lacking world writeable rights. So in other words only processes running as system may use the SystemClock approach. For this way android permissions do not matter, there's no entity involved which checks proper permissions.

This is the way the internal preinstalled Settings App works. It just runs under the system user account.

For all the other kids in town there's the alarm manager. It's a system service running in the system_server process under the - guess what - system user account. It exposes the mentioned setTime method but enforces the SET_TIME permission and in in turn just calls SystemClock.setCurrentTimeMillis internally (which succeeds because of the user the alarm manager is running as).

Cheers

3
  • Well, I have rooted my phone, but I'm not sure how to convince my application to run am.setTime() with the proper permissions. Do you know if there is a way??
    – Rich
    Jan 23, 2014 at 17:22
  • but i want get the time zone form server MDT and i want change the time zone like GMT so how it's possibal Jun 3, 2015 at 7:15
  • 1
    am.setTime(c.getTimeInMillis()); failed for me, on Nexus 6 with latest updates.
    – zeeshan
    Sep 9, 2016 at 19:46
23

According to this thread, user apps cannot set the time, regardless of the permissions we give it. Instead, the best approach is to make the user set the time manually. We will use:

startActivity(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS));

Unfortunately, there is no way to link them directly to the time setting (which would save them one more click). By making use of ellapsedRealtime, we can ensure that the user sets the time correctly.

16

A solution for rooted devices could be execute the commands

  • su
  • date -s YYYYMMDD.HHMMSS

Change date on cmd

You can do this by code with the following method:

private void changeSystemTime(String year,String month,String day,String hour,String minute,String second){
    try {
        Process process = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        String command = "date -s "+year+month+day+"."+hour+minute+second+"\n";
        Log.e("command",command);
        os.writeBytes(command);
        os.flush();
        os.writeBytes("exit\n");
        os.flush();
        process.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Just call the previous method like this:

changeSystemTime("2015","04","06","13","09","30");
3
  • I tested on android 6 and the default format is "MMDDhhmm[[CC]YY][.ss]" -s option should be followed by format instead of the actual time you wanna set. Not sure if this is android version difference.
    – York Yang
    Aug 30, 2017 at 0:17
  • 2
    How to set time zone ?
    – Jaya
    Mar 28, 2018 at 5:06
  • for newer android version this command need to change to date 2015-04-06T13:09:30Z Jan 19, 2021 at 5:00
3

I didn't see this one on the list anywhere but it works for me. My device is rooted and I have superuser installed, but if superuser works on non-rooted devices, this might work. I used an AsyncTask and called the following:

protected String doInBackground(String... params){
Runtime.getRuntime().exec("su && date -s " + params[0]);}
2

In our application case, the dirty workaround was:

When the user is connected to Internet, we get the Internet Time (NTP server) and compare the difference (-) of the internal device time (registederOffsetFromInternetTime). We save it on the config record file of the user.

We use the time of the devide + registederOffsetFromInternetTime to consider the correct updated time for OUR application.

All GETHOUR processes check the difference between the actual time with the time of the last comparission (with the Internet time). If the time over 10 minutes, do a new comparission to update registederOffsetFromInternetTime and mantain accuracy.

If the user uses the App without Internet, we can only use the registederOffsetFromInternetTime stored as reference, and use it. Just if the user changes the hour in local device when offline and use the app, the app will consider incorrect times. But when the user comes back to internet access we warn he about the clock changed , asking to resynchronize all or desconsider updates did offline with the incorrect hour.

-2

thanks penquin. In quickshortcutmaker I catch name of date/time seting activity exactly. so to start system time setting:

Intent intent=new Intent();
intent.setComponent(new ComponentName("com.android.settings",
         "com.android.settings.DateTimeSettingsSetupWizard"));      
startActivity(intent);

`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.