Java 以 12 小时格式显示当前时间,带 AM/PM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18734452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display current time in 12 hour format with AM/PM
提问by ronan
Currently the time displayed as 13:35 PMHowever I want to display as 12 hour format with AM/PM, i.e 1:35 PM instead of 13:35 PM
目前时间显示为13:35 PM但是我想以 AM/PM 显示为 12 小时格式,即 1:35 PM 而不是 13:35 PM
The current code is as below
当前代码如下
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";
}
采纳答案by Subhrajyoti Majumder
Easiest way to get it by using date pattern - h:mm a
, where
使用日期模式获取它的最简单方法 - h:mm a
,其中
- h - Hour in am/pm (1-12)
- m - Minute in hour
- a - Am/pm marker
- h - 上午/下午的小时 (1-12)
- m - 以小时为单位的分钟
- a - 上午/下午标记
Code snippet :
代码片段:
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
回答by Tarsem Singh
Use this SimpleDateFormat formatDate = new SimpleDateFormat("hh:mm a");
用这个 SimpleDateFormat formatDate = new SimpleDateFormat("hh:mm a");
回答by Ruchira Gayan Ranaweera
回答by MansoorShaikh
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.S aa");
String formattedDate = dateFormat.format(new Date()).toString();
System.out.println(formattedDate);
Output: 11-Sep-13 12.25.15.375 PM
输出:11-Sep-13 12.25.15.375 PM
回答by user2481237
Just replace below statement and it will work.
只需替换下面的语句,它就会起作用。
SimpleDateFormat formatDate = new SimpleDateFormat("hh:mm a");
回答by gopal
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
This will display the date and time
这将显示日期和时间
回答by alaguvel c
//To get Filename + date and time
SimpleDateFormat f = new SimpleDateFormat("MMM");
SimpleDateFormat f1 = new SimpleDateFormat("dd");
SimpleDateFormat f2 = new SimpleDateFormat("a");
int h;
if(Calendar.getInstance().get(Calendar.HOUR)==0)
h=12;
else
h=Calendar.getInstance().get(Calendar.HOUR)
String filename="TestReport"+f1.format(new Date())+f.format(new Date())+h+f2.format(new Date())+".txt";
The Output Like:TestReport27Apr3PM.txt
回答by Raju Ahmed
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
回答by Johnny K
SimpleDateFormat formatDate = new SimpleDateFormat("hh:mm:ss a");
h is used for AM/PM times (1-12).
H is used for 24 hour times (1-24).
a is the AM/PM marker
m is minute in hour
h 用于 AM/PM 时间 (1-12)。
H 用于 24 小时制 (1-24)。
a 是 AM/PM 标记
m 是分钟
Note: Two h's will print a leading zero: 01:13 PM. One h will print without the leading zero: 1:13 PM.
注意:两个小时将打印一个前导零:01:13 PM。将打印一个没有前导零的 h:1:13 PM。
Looks like basically everyone beat me to it already, but I digress
看起来基本上每个人都已经打败了我,但我离题了
回答by Mayank Bhatnagar
You can use SimpleDateFormat for this.
您可以为此使用 SimpleDateFormat。
SimpleDateFormat formatDate = new SimpleDateFormat("hh:mm a");
Hope this helps you.
希望这对你有帮助。