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

来源:互联网 发布:组策略禁用软件 编辑:程序博客网 时间:2024/04/30 06:55

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

[java] view plaincopy
  1. ContentResolver cv = this.getContentResolver();  
  2.        String strTimeFormat = android.provider.Settings.System.getString(cv,  
  3.                                           android.provider.Settings.System.TIME_12_24);  
  4.          
  5.        if(strTimeFormat.equals("24"))  
  6.   
  7.       {  
  8.              Log.i("activity","24");  
  9.        }  
以上来自:http://www.cnblogs.com/liwqiang/archive/2010/08/08/1795172.html,感谢他的分享,解决了我当时的问题。


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

[java] view plaincopy
  1. /** 
  2.      * @return true if clock is set to 24-hour mode 
  3.      */  
  4.     public static boolean get24HourMode(final Context context) {  
  5.         return android.text.format.DateFormat.is24HourFormat(context);  
  6.     }  

进入源码

[java] view plaincopy
  1. /** 
  2.      * Returns true if user preference is set to 24-hour format. 
  3.      * @param context the context to use for the content resolver 
  4.      * @return true if 24 hour time format is selected, false otherwise. 
  5.      */  
  6.     public static boolean is24HourFormat(Context context) {  
  7.         String value = Settings.System.getString(context.getContentResolver(),  
  8.                 Settings.System.TIME_12_24);  
  9.   
  10.         if (value == null) {  
  11.             Locale locale = context.getResources().getConfiguration().locale;  
  12.   
  13.             synchronized (sLocaleLock) {  
  14.                 if (sIs24HourLocale != null && sIs24HourLocale.equals(locale)) {  
  15.                     return sIs24Hour;  
  16.                 }  
  17.             }  
  18.   
  19.             java.text.DateFormat natural =  
  20.                 java.text.DateFormat.getTimeInstance(java.text.DateFormat.LONG, locale);  
  21.   
  22.             if (natural instanceof SimpleDateFormat) {  
  23.                 SimpleDateFormat sdf = (SimpleDateFormat) natural;  
  24.                 String pattern = sdf.toPattern();  
  25.   
  26.                 if (pattern.indexOf('H') >= 0) {  
  27.                     value = "24";  
  28.                 } else {  
  29.                     value = "12";  
  30.                 }  
  31.             } else {  
  32.                 value = "12";  
  33.             }  
  34.   
  35.             synchronized (sLocaleLock) {  
  36.                 sIs24HourLocale = locale;  
  37.                 sIs24Hour = value.equals("24");  
  38.             }  
  39.   
  40.             return sIs24Hour;  
  41.         }  
  42.   
  43.         return value.equals("24");  
  44.     }  
0 0
原创粉丝点击