现在的位置: 首页 > 综合 > 正文

获取Android系统时间是24小时制还是12小时制

2018年02月15日 ⁄ 综合 ⁄ 共 1464字 ⁄ 字号 评论关闭

这个是在网上能搜到的方法,用来判断当前系统是12小时制或者24小时制,当时做项目用的就是这个判断。

 ContentResolver cv = this.getContentResolver();
        String strTimeFormat = android.provider.Settings.System.getString(cv,
                                           android.provider.Settings.System.TIME_12_24);
        
        if(strTimeFormat.equals("24"))

       {
              Log.i("activity","24");
        }

以上来自:http://www.cnblogs.com/liwqiang/archive/2010/08/08/1795172.html,感谢他的分享,解决了我当时的问题。

最近没事看了看DeskClock的源码,里边也有一个判断是否是24小时制的方法,API提供的,写的更加完善,考虑的更加周全。

/**
     * @return true if clock is set to 24-hour mode
     */
    public static boolean get24HourMode(final Context context) {
        return android.text.format.DateFormat.is24HourFormat(context);
    }

进入源码

/**
     * Returns true if user preference is set to 24-hour format.
     * @param context the context to use for the content resolver
     * @return true if 24 hour time format is selected, false otherwise.
     */
    public static boolean is24HourFormat(Context context) {
        String value = Settings.System.getString(context.getContentResolver(),
                Settings.System.TIME_12_24);

        if (value == null) {
            Locale locale = context.getResources().getConfiguration().locale;

            synchronized (sLocaleLock) {
                if (sIs24HourLocale != null && sIs24HourLocale.equals(locale)) {
                    return sIs24Hour;
                }
            }

            java.text.DateFormat natural =
                java.text.DateFormat.getTimeInstance(java.text.DateFormat.LONG, locale);

            if (natural instanceof SimpleDateFormat) {
                SimpleDateFormat sdf = (SimpleDateFormat) natural;
                String pattern = sdf.toPattern();

                if (pattern.indexOf('H') >= 0) {
                    value = "24";
                } else {
                    value = "12";
                }
            } else {
                value = "12";
            }

            synchronized (sLocaleLock) {
                sIs24HourLocale = locale;
                sIs24Hour = value.equals("24");
            }

            return sIs24Hour;
        }

        return value.equals("24");
    }

抱歉!评论已关闭.