SimpleDateFormat 中使用 a 和 aaa 有什么区别

发布于 2024-11-05 01:22:35 字数 371 浏览 7 评论 0原文

我想将当前日期显示为 00:50:32 A

这是我的代码

Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss a");
String time = sdf.format(date);
System.out.println("time : " + time);

,但它打印为:

time : 00:50:32 AM

我尝试了 HH:mm:ss aHH :mm:ss aaa,但结果是一样的。

I want to display current date as 00:50:32 A

Here is my code

Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss a");
String time = sdf.format(date);
System.out.println("time : " + time);

But it print as:

time : 00:50:32 AM

I tried both HH:mm:ss a and HH:mm:ss aaa, but results are the same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

方觉久 2024-11-12 01:22:35

做不到!如果模式不超过 4 个字母,则使用短格式。所以 'a'、'aa'、'aaa' 和 'aaaa' 是相同的。

您所能做的就是将其格式化为不带“a”,然后手动添加“A”或“P”。

话虽如此,使用“HH”(24 小时制)为什么需要 AM/PM?

Can't do! If the pattern is 4 letters or less, short format is used. So 'a', 'aa', 'aaa' and 'aaaa' are identical.

All you can do is to format it without 'a', and add 'A' or 'P' manually.

Having said that, using 'HH' (24-hours clock) why would you need AM/PM?

末蓝 2024-11-12 01:22:35

我相信对于上午/下午标记来说,“短”和“长”之间没有区别。

特别是 DateFormatSymbols .getAmPmStrings() 仅返回两个字符串 - 并且没有 getShortAmPmStrings() 调用或类似的内容。

I believe for am/pm markers, there's no difference between "short" and "long".

In particular, DateFormatSymbols.getAmPmStrings() returns just two strings - and there's no getShortAmPmStrings() call or anything like that.

偏爱自由 2024-11-12 01:22:35

引用SimpleDateFormat的Javadoc

对于格式化,如果数量
图案字母为4个或以上,满
使用表格;否则短路或
如果可用,则使用缩写形式

因此:
(a) 如果您希望看到差异,请使用 aaaa (4 x a) 而不是 aaa (4 x a代码>)。
(b) 鉴于 AM/PM 没有短格式(或长格式),那么对于 a 说明符来说,重复次数并不重要。

为了更彻底一点,我运行了以下程序。它发现格式受到影响的案例为零。

Date date = new Date();
int n = 0;
for (String country : Locale.getISOCountries()) {
  for (String language : Locale.getISOLanguages()) {
    Locale loc = new Locale(language, country);
    String as = "";
    String prev = null;
    for (int i = 0; i < 20; ++i) {
      ++n;
      as += "a";
      String current = new SimpleDateFormat(as, loc).format(date);
      if (prev != null && !prev.equals(current)) {
        System.out.println("Locale: " + loc + ", as=" + as + ", current="
          + prev + ", next=" + current);
      }

      prev = current;
    }
  }
}
System.out.println("Tried out " + n + " combinations.");    

Quoting from the Javadoc of SimpleDateFormat:

For formatting, if the number of
pattern letters is 4 or more, the full
form is used; otherwise a short or
abbreviated form is used if available

Thus:
(a) If you expect to see a difference use aaaa (4 x a) rather than aaa (4 x a).
(b) Given that AM/PM has no short form (or long form) then for the a specifier the number of repetition does not matter.

And just to be a bit more thorough, I ran the following program. It found zero cases where the formatting was affected.

Date date = new Date();
int n = 0;
for (String country : Locale.getISOCountries()) {
  for (String language : Locale.getISOLanguages()) {
    Locale loc = new Locale(language, country);
    String as = "";
    String prev = null;
    for (int i = 0; i < 20; ++i) {
      ++n;
      as += "a";
      String current = new SimpleDateFormat(as, loc).format(date);
      if (prev != null && !prev.equals(current)) {
        System.out.println("Locale: " + loc + ", as=" + as + ", current="
          + prev + ", next=" + current);
      }

      prev = current;
    }
  }
}
System.out.println("Tried out " + n + " combinations.");    
阳光的暖冬 2024-11-12 01:22:35

没有什么区别,你期望有什么不同?

基于此网站(http://javatechniques.com/blog/dateformat-and- simpledateformat-examples/):

“一”-> “上午”

“aa”-> “上午”

There is no difference, what were you expecting to differ?

Based on this site (http://javatechniques.com/blog/dateformat-and-simpledateformat-examples/):

“a” -> “AM”

“aa” -> “AM”

半﹌身腐败 2024-11-12 01:22:35

那么你的选择是

time = time.substring(0,time.length()-1);

:) 愚蠢但可行

Then the option you have is

time = time.substring(0,time.length()-1);

:) Silly but will work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文