android 显示时间显示上下午,Android4.0 时间显示12小时制没有显示AM、PM

这篇博客主要介绍了如何修改Android应用中的时间显示,包括HoloClock.java和DigitalClock.java类中的getTimeText()和updateTime()方法,以根据24小时制或12小时制添加AM/PM标识。通过判断系统设置来决定时间格式,并更新UI。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

修改状态栏中控制时间的类HoloClock.java的方法getTimeText()进行修改,判断时间是否为24小时制,若是则直接返回时间若不是则判断是上午还是下午,上午的话添加AM,下午添加PM。

private final CharSequence getTimeText() {

Context context = getContext();

int res = DateFormat.is24HourFormat(context)

? com.android.internal.R.string.twenty_four_hour_time_format

: com.android.internal.R.string.twelve_hour_time_format;

SimpleDateFormat sdf;

String format = context.getString(res);

if (!format.equals(mClockFormatString)) {

// we don't want AM/PM showing up in our statusbar, even in 12h mode

format = format.replaceAll("a", "").trim();

mClockFormat = sdf = new SimpleDateFormat(format);

mClockFormatString = format;

} else {

sdf = mClockFormat;

}

String result = sdf.format(mCalendar.getTime());

//2012-9-14 START can not show AM\BM in 12hour

//return result;

if(DateFormat.is24HourFormat(context)){

return result;

}else{

String ampmValues;

if(mCalendar.get(Calendar.AM_PM) == 0){

ampmValues = "AM";

}else{

ampmValues = "PM";

}

return result+" "+ampmValues;

}

//2012-9-14 END can not show AM\BM in 12hour

}

修改锁屏界面控制时间的类DigitalClock.java的方法updateTime(),同上判断时间是否为24小时制和是上午还是下午。

private void updateTime() {

mCalendar.setTimeInMillis(System.currentTimeMillis());

CharSequence newTime = DateFormat.format(mFormat, mCalendar);

//2012-9-14 START can not show AM\BM in 12hour

Context context = getContext();

String ampmValues;

if(DateFormat.is24HourFormat(context)){

ampmValues = "";

}else{

if(mCalendar.get(Calendar.AM_PM) == 0){

ampmValues = " AM";

}else{

ampmValues = " PM";

}

}

newTime = newTime+ampmValues;

//2012-9-14 END can not show AM\BM in 12hour

mTimeDisplayBackground.setText(newTime);

mTimeDisplayForeground.setText(newTime);

mAmPm.setIsMorning(mCalendar.get(Calendar.AM_PM) == 0);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值