目前时间显示为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";
}
通过使用日期模式获得它的最简单方法 - h:mm a
,在哪里
代码段:
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
这将显示日期和时间
如果您想要使用AM当前时间,请使用Android中的PM
String time = new SimpleDateFormat("hh : mm a", Locale.getDefault()).format(Calendar.getInstance().getTime());
如果你想要当前时间am,pm
String time = new SimpleDateFormat("hh : mm a", Locale.getDefault()).format(Calendar.getInstance().getTime()).toLowerCase();
要么
从API级别26开始
LocalTime localTime = LocalTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm a");
String time = localTime.format(dateTimeFormatter);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
将您当前的移动日期和时间格式放入
2018年2月9日下午10:36:59
Date date = new Date();
String stringDate = DateFormat.getDateTimeInstance().format(date);
你可以通过使用Activity
向你的qazxsw poi,qazxsw poi,qazxsw poi,qazxsw poi展示它
Fragment
CardView
您可以使用SimpleDateFormat。
ListView
希望这对你有所帮助。
使用这个SimpleDateFormat formatDate = new SimpleDateFormat("hh:mm a");
使用"hh:mm a"
而不是"HH:mm a"
。这里hh
为12小时格式和HH
为24小时格式。
住Demo
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.S aa");
String formattedDate = dateFormat.format(new Date()).toString();
System.out.println(formattedDate);
产出:11月11日至11日12.25.15.375 PM
SimpleDateFormat formatDate = new SimpleDateFormat("hh:mm:ss a");
注意:两个h将打印一个前导零:01:13 PM。一个h将打印没有前导零:下午1:13。
看起来基本上每个人都打败了我,但我离题了
// 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()));
只需替换以下声明即可。
SimpleDateFormat formatDate = new SimpleDateFormat("hh:mm a");
使用Java 8:
LocalTime localTime = LocalTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm a");
System.out.println(localTime.format(dateTimeFormatter));
输出采用AM/PM
格式。
Sample output: 3:00 PM
//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