Android時間時區設置和獲取

判斷系統是否自動獲取時區

public static boolean isTimeZoneAuto(Context mContext) {
        try {
            return android.provider.Settings.Global.getInt(mContext.getContentResolver(),
                    android.provider.Settings.Global.AUTO_TIME_ZONE) > 0;
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }

設置自動獲取時區

public static void setAutoTimeZone(Context mContext, int checked) {
        android.provider.Settings.Global.putInt(mContext.getContentResolver(),
                android.provider.Settings.Global.AUTO_TIME_ZONE, checked);
    }

獲取系統默認時區

  public static String getTimeZone() {
        return TimeZone.getDefault().getDisplayName(false, TimeZone.SHORT);
    }

設置系統默認時區

// 需要添加權限<uses-permission android:name="android.permission.SET_TIME_ZONE" />
 public static void setChinaTimeZone(Context context) {
       /* TimeZone.getTimeZone("GMT+8");
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));*/
        AlarmManager mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        mAlarmManager.setTimeZone("Asia/Shanghai");// Asia/Taipei//GMT+08:00
    }

判斷系統是否自動獲取日期和時間

        try {
            return android.provider.Settings.Global.getInt(mContext.getContentResolver(),
                    android.provider.Settings.Global.AUTO_TIME) > 0;
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }

設置系統自動獲取日期和時間

public static void setAutoDateTime(Context mContext, int checked) {
        android.provider.Settings.Global.putInt(mContext.getContentResolver(),
                android.provider.Settings.Global.AUTO_TIME, checked);
    }

獲取系統當前時間

  public static String currentTime() {
        long currentTime = System.currentTimeMillis();
        Date date = new Date(currentTime);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        return formatter.format(date);
    }

設置系統當前時間


    // 需要系統權限
    public static boolean setSytemTime(long value){
        long settingTime = value;
        if (value/1000 < Integer.MAX_VALUE){
            SystemClock.setCurrentTimeMillis(settingTime);
        }
    }

判斷系統是否是24h格式(12h同理)

public static boolean is24Hour(Context mContext) {
        ContentResolver cv = mContext.getContentResolver();
        String strTimeFormat = android.provider.Settings.System.getString(cv,
                android.provider.Settings.System.TIME_12_24);
        if (strTimeFormat != null && strTimeFormat.equals("24")) {
            return true;
        }
        return false;
    }

設置系統爲24h格式(12h同理)

 public static void set24Hour(Context mContext) {
        ContentResolver cv = mContext.getContentResolver();
        android.provider.Settings.System.putString(cv, Settings.System.TIME_12_24, "24");
    }


說明:系統時區和時間是否自動獲取是作爲參數存儲到數據庫的,所以只需要修改參數即選中是否同步時間
參考:http://www.jianshu.com/p/6c6a6091545d

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章