使用AM/PM以12小时格式显示当前时间

ron*_*nan 171 java date-format

目前时间显示为13:35 PM 但是我希望以AM/PM显示为12小时格式,即下午1:35而不是下午13:35

目前的代码如下

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";
}
Run Code Online (Sandbox Code Playgroud)

Sub*_*der 429

通过使用日期模式获取它的最简单方法 - h:mm a,在哪里

  • h - 上午/下午(1-12)小时
  • m - 小时分钟
  • a - Am/pm标记

代码段:

DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
Run Code Online (Sandbox Code Playgroud)

阅读更多文档 - SimpleDateFormat java 7

  • 请注意,使用两个h("hh")会给出一个前导零(即01:23 AM).一个"h"给你一个没有前导零的小时(凌晨1:23). (51认同)
  • 如何获得AM&PM而不是'am'和'pm' (6认同)

Tar*_*ngh 105

用这个 SimpleDateFormat formatDate = new SimpleDateFormat("hh:mm a");

在此输入图像描述

SimpleDateFormat的Java文档


Ruc*_*era 58

"hh:mm a"而不是"HH:mm a".这里hh有12小时格式和HH24小时格式.

现场演示


Man*_*ikh 19

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.S aa");
String formattedDate = dateFormat.format(new Date()).toString();
System.out.println(formattedDate);
Run Code Online (Sandbox Code Playgroud)

产出:11月11日至11日12.25.15.375 PM


小智 15

SimpleDateFormat formatDate = new SimpleDateFormat("hh:mm:ss a");
Run Code Online (Sandbox Code Playgroud)
  • h用于AM/PM时间(1-12).

  • H使用24小时(1-24).

  • a是AM/PM标记

  • m是分钟

注意:两个h将打印一个前导零:01:13 PM.一个h将打印没有前导零:下午1:13.

看起来基本上每个人都打败了我,但我离题了


小智 9

// hh:mm will print hours in 12hrs clock and mins (e.g. 02:30)
System.out.println(DateTimeFormatter.ofPattern("hh:mm").format(LocalTime.now()));

// HH:mm will print hours in 24hrs clock and mins (e.g. 14:30)
System.out.println(DateTimeFormatter.ofPattern("HH:mm").format(LocalTime.now())); 

// hh:mm a will print hours in 12hrs clock, mins and AM/PM (e.g. 02:30 PM)
System.out.println(DateTimeFormatter.ofPattern("hh:mm a").format(LocalTime.now())); 
Run Code Online (Sandbox Code Playgroud)


Bas*_*que 9

太长了;博士

\n\n

让 JSR 310 的现代java.time类自动生成本地化文本,而不是硬编码 12 小时时钟和 AM/PM。

\n\n
LocalTime                                     // Represent a time-of-day, without date, without time zone or offset-from-UTC.\n.now(                                         // Capture the current time-of-day as seen in a particular time zone.\n    ZoneId.of( "Africa/Casablanca" )          \n)                                             // Returns a `LocalTime` object.\n.format(                                      // Generate text representing the value in our `LocalTime` object.\n    DateTimeFormatter                         // Class responsible for generating text representing the value of a java.time object.\n    .ofLocalizedTime(                         // Automatically localize the text being generated.\n        FormatStyle.SHORT                     // Specify how long or abbreviated the generated text should be.\n    )                                         // Returns a `DateTimeFormatter` object.\n    .withLocale( Locale.US )                  // Specifies a particular locale for the `DateTimeFormatter` rather than rely on the JVM\xe2\x80\x99s current default locale. Returns another separate `DateTimeFormatter` object rather than altering the first, per immutable objects pattern.\n)                                             // Returns a `String` object.\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

上午 10:31

\n
\n\n

自动本地化

\n\n

您可能希望让java.time自动为您进行本地化,而不是坚持采用 AM/PM 的 12 小时制。称呼DateTimeFormatter.ofLocalizedTime

\n\n

要本地化,请指定:

\n\n
    \n
  • FormatStyle确定字符串的长度或缩写。
  • \n
  • Locale确定:\n\n
      \n
    • 用于翻译日名、月名等的人类语言
    • \n
    • 决定缩写、大写、标点、分隔符等问题的文化规范
    • \n
  • \n
\n\n

在这里,我们获取特定时区的当前时间。然后我们生成文本来表示那个时间。我们在加拿大文化中本地化法语,然后在美国文化中本地化英语。

\n\n
ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;\nLocalTime localTime = LocalTime.now( z ) ;\n\n// Qu\xc3\xa9bec\nLocale locale_fr_CA = Locale.CANADA_FRENCH ;  // Or `Locale.US`, and so on.\nDateTimeFormatter formatterQu\xc3\xa9bec = DateTimeFormatter.ofLocalizedTime( FormatStyle.SHORT ).withLocale( locale_fr_CA ) ;\nString outputQu\xc3\xa9bec = localTime.format( formatterQu\xc3\xa9bec ) ;\n\nSystem.out.println( outputQu\xc3\xa9bec ) ;\n\n// US\nLocale locale_en_US = Locale.US ;  \nDateTimeFormatter formatterUS = DateTimeFormatter.ofLocalizedTime( FormatStyle.SHORT ).withLocale( locale_en_US ) ;\nString outputUS = localTime.format( formatterUS ) ;\n\nSystem.out.println( outputUS ) ;\n
Run Code Online (Sandbox Code Playgroud)\n\n

请参阅在 IdeOne.com 上实时运行的代码

\n\n
\n

10 小时 31 分

\n\n

上午 10:31

\n
\n


小智 7

使用Java 8:

LocalTime localTime = LocalTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm a");
System.out.println(localTime.format(dateTimeFormatter));
Run Code Online (Sandbox Code Playgroud)

输出AM/PM格式为.

Sample output:  3:00 PM
Run Code Online (Sandbox Code Playgroud)


小智 5

只需替换以下声明即可.

SimpleDateFormat formatDate = new SimpleDateFormat("hh:mm a");
Run Code Online (Sandbox Code Playgroud)


Mar*_*Gin 5

如果您想在Android 中使用AM、PM 的当前时间

String time = new SimpleDateFormat("hh : mm a", Locale.getDefault()).format(Calendar.getInstance().getTime());
Run Code Online (Sandbox Code Playgroud)

如果您想要 am, pm 的当前时间

String time = new SimpleDateFormat("hh : mm a", Locale.getDefault()).format(Calendar.getInstance().getTime()).toLowerCase();
Run Code Online (Sandbox Code Playgroud)

或者

从 API 级别 26

LocalTime localTime = LocalTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm a");
String time = localTime.format(dateTimeFormatter);
Run Code Online (Sandbox Code Playgroud)

  • 在 2019 年,请不要教年轻人使用已经过时且臭名昭著的“SimpleDateFormat”类。至少不是第一选择。并非没有任何保留。今天,我们在 [`java.time`,现代 Java 日期和时间 API](https://docs.oracle.com/javase/tutorial/datetime/) 及其 `DateTimeFormatter` 方面有了更好的表现。 (3认同)