问题
目前时间显示为13:35 PMHowever我想用AM / PM显示12小时格式,即下午1:35而不是13:35 PM
目前的代码如下
private static final int FOR_HOURS = 3600000;
private static final int FOR_MIN = 60000;
public String getTime(final Model model) {
SimpleDateFormat formatDate = new SimpleDateFormat("HH:mm a");
formatDate.setTimeZone(userContext.getUser().getTimeZone());
model.addAttribute("userCurrentTime", formatDate.format(new Date()));
final String offsetHours = String.format("%+03d:%02d", userContext.getUser().getTimeZone().getRawOffset()
/ FOR_HOURS, Math.abs(userContext.getUser().getTimeZone().getRawOffset() % FOR_HOURS / FOR_MIN));
model.addAttribute("offsetHours",
offsetHours + " " + userContext.getUser().getTimeZone().getDisplayName(Locale.ROOT));
return "systemclock";
}
#1 热门回答(329 赞)
最简单的方法是使用日期模式-h:mm a
,其中
- h - 上午/下午(1-12)
- 米 - 小时分钟
- a - Am / pm标记
代码段:
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
Read more on documentation - SimpleDateFormat java 7
#2 热门回答(91 赞)
使用此SimpleDateFormat formatDate = new SimpleDateFormat("hh:mm a");
Java docs for SimpleDateFormat
#3 热门回答(45 赞)
使用"hh:mm a"
而不是"HH:mm a"
。这里hh
为12小时格式,HH
为24小时格式。
LiveDemo