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

  • Post author:
  • Post category:其他


修改状态栏中控制时间的类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);

}