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,在哪里
代码段:
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
Run Code Online (Sandbox Code Playgroud)
阅读更多文档 - SimpleDateFormat java 7
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)
让 JSR 310 的现代java.time类自动生成本地化文本,而不是硬编码 12 小时时钟和 AM/PM。
\n\nLocalTime // 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.\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\n上午 10:31
\n
您可能希望让java.time自动为您进行本地化,而不是坚持采用 AM/PM 的 12 小时制。称呼DateTimeFormatter.ofLocalizedTime。
要本地化,请指定:
\n\nFormatStyle确定字符串的长度或缩写。Locale确定:\n\n在这里,我们获取特定时区的当前时间。然后我们生成文本来表示那个时间。我们在加拿大文化中本地化法语,然后在美国文化中本地化英语。
\n\nZoneId 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 ) ;\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\n\n10 小时 31 分
\n\n上午 10:31
\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)
如果您想在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)
| 归档时间: |
|
| 查看次数: |
356636 次 |
| 最近记录: |