以 12 小时格式显示当前时间,带 AM/PM [英] Display current time in 12 hour format with AM/PM

查看:47
本文介绍了以 12 小时格式显示当前时间,带 AM/PM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前时间显示为下午 13:35但是我想以 12 小时格式显示 AM/PM,即 1:35 PM 而不是 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";
}

推荐答案

使用日期模式最简单的方法 - h:mm a, where

Easiest way to get it by using date pattern - h:mm a, where

  • h - 上午/下午的小时数 (1-12)
  • m - 以小时为单位的分钟
  • a - 上午/下午标记

代码片段:

DateFormat dateFormat = new SimpleDateFormat("hh:mm a");

阅读有关文档的更多信息 - SimpleDateFormat java 7

这篇关于以 12 小时格式显示当前时间,带 AM/PM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆