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

问题描述 投票:154回答:15

目前时间显示为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";
}
java date-format
15个回答
402
投票

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

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

代码段:

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

Read more on documentation - SimpleDateFormat java 7


2
投票
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm a");

这将显示日期和时间


2
投票

如果您想要使用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);

0
投票
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a");

0
投票

将您当前的移动日期和时间格式放入

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

0
投票
CardView

-2
投票

您可以使用SimpleDateFormat。

ListView

希望这对你有所帮助。


102
投票

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

Java docs for SimpleDateFormat


55
投票

使用"hh:mm a"而不是"HH:mm a"。这里hh为12小时格式和HH为24小时格式。

Demo


19
投票
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


14
投票
SimpleDateFormat formatDate = new SimpleDateFormat("hh:mm:ss a");
  • h用于AM / PM时间(1-12)。
  • H使用24小时(1-24)。
  • a是AM / PM标记
  • m是分钟

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

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


8
投票
// 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())); 

6
投票

只需替换以下声明即可。

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

6
投票

使用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

3
投票
    //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
© www.soinside.com 2019 - 2024. All rights reserved.