Android 12小时制和24小时制的切换

在开发中会涉及到对12小时和24小时的切换,查阅资料终于找到解决方案,代码如下:
1.获取当前小时制:

private String getTimeformat(){
      return  Settings.System.getString(mContext.getContentResolver(),
                Settings.System.TIME_12_24);
    }
1
2
3
4
2.切换时区

Settings.System.putString(mContext.getContentResolver(),
                    Settings.System.TIME_12_24,
                    isChecked ? "24" : "12");
            Intent timeChanged = new Intent(Intent.ACTION_TIME_CHANGED);
            int timeFormatPreference =
                    isChecked ? Intent.EXTRA_TIME_PREF_VALUE_USE_24_HOUR
                            : Intent.EXTRA_TIME_PREF_VALUE_USE_12_HOUR;
            timeChanged.putExtra(Intent.EXTRA_TIME_PREF_24_HOUR_FORMAT, timeFormatPreference);
            mContext.sendBroadcast(timeChanged);
1
2
3
4
5
6
7
8
9
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            updateTimeAndDateDisplay(mContext);
        }
    };
1
2
3
4
5
6
 IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_TICK);
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        mContext.registerReceiver(mIntentReceiver, filter);
1
2
3
4
5
private void updateTimeAndDateDisplay(Context context) {
        String format = getTimeformat();
        final Calendar now = Calendar.getInstance();
        String dateform="";
        if(!TextUtils.isEmpty(format)) {
            if (format.equals("12")) {
                dateform = "hh:mmaa";
            } else {
                dateform = "HH:mm";
            }
        }else{
            dateform = "HH:mm"; 
        }
        SimpleDateFormat sdf = new SimpleDateFormat(dateform);
        String currenttime = sdf.format(now.getTime());
        Log.e("MainActivity","currenttime:"+currenttime+"---format:"+format);
        if(!TextUtils.isEmpty(format)) {
            if (format.equals("12")) {
                if (currenttime.contains("上午")) {
                    currenttime = "上午" + currenttime.substring(0, currenttime.indexOf("上午"));
                } else {
                    currenttime = "下午" + currenttime.substring(0, currenttime.indexOf("下午"));
                }
            }
        }
        time_textview.setText(currenttime);
//        mTimeZone.setSummary(ZoneGetter.getTimeZoneOffsetAndName(getActivity(),
//                now.getTimeZone(), now.getTime()));
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
就这样就可以切换小时制了
————————————————
版权声明:本文为CSDN博主「Android framework工程师」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/baidu_41666295/article/details/105840438

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值