Set system date time and time zone
Setting the date, time and time zone of the system requires system permission and system signature. android:sharedUserId="android.uid.system"
You need to add the corresponding permissions in the manifest file
<uses-permission android:name="android.permission.WRITE_SETTINGS"/> <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
-
Determine whether the system uses the 24-hour system or the 12-hour system
boolean is24Hour = DateFormat.is24HourFormat(mContext);
- Set the hour system of the system
24 hour system
android.provider.Settings.System.putString(mContext.getContentResolver(), android.provider.Settings.System.TIME_12_24, "24");
12 hour system
android.provider.Settings.System.putString(mContext.getContentResolver(), android.provider.Settings.System.TIME_12_24, "12");
-
Determine whether the time zone of the system is automatically obtained
public boolean isTimeZoneAuto(){ try { return android.provider.Settings.Global.getInt(mContext.getContentResolver(), android.provider.Settings.Global.AUTO_TIME_ZONE) > 0; } catch (SettingNotFoundException e) { e.printStackTrace(); return false; } }
-
Set whether the time zone of the system is automatically obtained
public void setAutoTimeZone(int checked){ android.provider.Settings.Global.putInt(mContext.getContentResolver(), android.provider.Settings.Global.AUTO_TIME_ZONE, checked); }
-
Determine whether the system time is automatically obtained
public boolean isDateTimeAuto(){ try { return android.provider.Settings.Global.getInt(mContext.getContentResolver(), android.provider.Settings.Global.AUTO_TIME) > 0; } catch (SettingNotFoundException e) { e.printStackTrace(); return false; } }
-
Set whether the time of the system needs to be acquired automatically
public void setAutoDateTime(int checked){ android.provider.Settings.Global.putInt(mContext.getContentResolver(), android.provider.Settings.Global.AUTO_TIME, checked); }
-
Set system date
Source code in reference system Settings
public void setSysDate(int year,int month,int day){ Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month); c.set(Calendar.DAY_OF_MONTH, day); long when = c.getTimeInMillis(); if(when / 1000 < Integer.MAX_VALUE){ ((AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE)).setTime(when); } }
-
Set system time
Source code in reference system Settings
public void setSysTime(int hour,int minute){ Calendar c = Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY, hour); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); long when = c.getTimeInMillis(); if(when / 1000 < Integer.MAX_VALUE){ ((AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE)).setTime(when); } }
-
Set system time zone
public void setTimeZone(String timeZone){ final Calendar now = Calendar.getInstance(); TimeZone tz = TimeZone.getTimeZone(timeZone); now.setTimeZone(tz); }
-
Get the current time zone of the system
public String getDefaultTimeZone(){ return TimeZone.getDefault().getDisplayName(); }