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

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

[java]  view plain copy
  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 plain copy
  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 plain copy
  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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值