SimpleDateFormat 中使用 a 和 aaa 有什么区别
我想将当前日期显示为 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 a
和 HH :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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
做不到!如果模式不超过 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?
我相信对于上午/下午标记来说,“短”和“长”之间没有区别。
特别是
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 nogetShortAmPmStrings()
call or anything like that.引用SimpleDateFormat的Javadoc:
因此:
(a) 如果您希望看到差异,请使用
aaaa
(4 xa
) 而不是aaa
(4 xa
代码>)。(b) 鉴于 AM/PM 没有短格式(或长格式),那么对于
a
说明符来说,重复次数并不重要。为了更彻底一点,我运行了以下程序。它发现格式受到影响的案例为零。
Quoting from the Javadoc of SimpleDateFormat:
Thus:
(a) If you expect to see a difference use
aaaa
(4 xa
) rather thanaaa
(4 xa
).(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.
没有什么区别,你期望有什么不同?
基于此网站(http://javatechniques.com/blog/dateformat-and- simpledateformat-examples/):
There is no difference, what were you expecting to differ?
Based on this site (http://javatechniques.com/blog/dateformat-and-simpledateformat-examples/):
那么你的选择是
:) 愚蠢但可行
Then the option you have is
:) Silly but will work