Java hh:mm a 和 HH:mm a 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34431260/
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
Difference between hh:mm a and HH:mm a
提问by AlwaysALearner
Here is my original code-
这是我的原始代码-
String dateString = "23 Dec 2015 1:4 PM";
Locale locale = new Locale("en_US");
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm a");
DateFormat df = new SimpleDateFormat("dd MMM yyyy HH:mm a", locale);
Date date = null;
try {
date = formatter.parse(dateString);
} catch (ParseException e) {
LOGGER.error(e);
}
String newDate = df.format(date);
System.out.println("oldDate = " + dateString);
System.out.println("newDate = " + newDate);
and here is my output-
这是我的输出-
oldDate = 23 Dec 2015 1:4 PM
newDate = 23 Dec 2015 01:04 AM
There is AM-PM differencebetween the oldDate
and newDate
. Now I changed the DateFormat
code to-
和之间存在AM-PM 差异。现在我将代码更改为-oldDate
newDate
DateFormat
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy hh:mm a");
DateFormat df = new SimpleDateFormat("dd MMM yyyy hh:mm a", locale);
and I get the expected output, which is-
我得到了预期的输出,即-
oldDate = 23 Dec 2015 1:4 PM
newDate = 23 Dec 2015 01:04 PM
I'm aware that HH
signifies the 24 hourformat and hh
signifies the 12-hourformat.
我知道HH
表示24 小时格式并hh
表示12 小时格式。
My question is
我的问题是
If I use HH:mm a
instead of hh:mm a
, shouldn't this be returning the time in 24-hourformat?
如果我使用HH:mm a
而不是hh:mm a
,这不应该以24 小时格式返回时间吗?
(or)
(或者)
If it defaults to 12-hourformat, shouldn't it return respective AM/PM marker depending on the date input provided?
如果它默认为12 小时格式,是否应该根据提供的日期输入返回相应的 AM/PM 标记?
This is just for my understanding.
这只是为了我的理解。
采纳答案by Mr.Arjun
Updated Answer
更新答案
Here the problem is with Program flow
这里的问题是 Program flow
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm a");
this simple date formatter
is used to format the date string the Date objectis created, here is the important part of answer
这simple date formatter
用于格式化日期对象创建的日期字符串,这是答案的重要部分
As you are using HHinstead of hh, the SimpleDateFormaterconsiders that provided date string is in 24-Hour Format
and simply ignoresthe AM/PM marker
here.
当您使用HH代替HH的SimpleDateFormater认为提供日期字符串是24-Hour Format
,只是忽略了AM/PM marker
这里。
The Date Object from constructed from this SimpleDateFormatter is passed to the
从此 SimpleDateFormatter 构造的日期对象传递给
DateFormat df = new SimpleDateFormat("dd MMM yyyy HH:mm a", locale);
That's why it's printing
这就是它打印的原因
newDate = 23 Dec 2015 01:04 AM
if you change the line
如果你改变线
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm a");
with
和
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy hh:mm a");
Everything will go smooth as it should!
一切都会顺利的!
Note: when creating a locale you should pass the language code to the Locale() constructor. en-us
not en_us
.
注意:创建语言环境时,您应该将语言代码传递给 Locale() 构造函数。en-us
不是en_us
。
IMP: Code is tested on Java 8also. Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
IMP:代码也在Java 8上进行了测试。Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
回答by Abdelhak
Difference between hh:mm and HH:mm:
hh:mm 和 HH:mm 的区别:
HH:mm => will look like 00:12, 23:00... this has 24 hour format.
hh:mm => will look like 01:00Am, 02:00Am,...01:00pm this has 12 hour format.
And the a
in hh:mm a or in HH:mm a is Am/pm marker for more info go to link
并且a
in hh:mm a 或 in HH:mm a 是 Am/pm 标记以获取更多信息,请访问链接
回答by Meno Hochschild
A simple test is the best answer to how the pattern "HH:mm a" is interpreted. First let us investigate the printing mode:
一个简单的测试是如何解释模式“HH:mm a”的最佳答案。首先让我们研究一下打印模式:
The old format engine SimpleDateFormat
:
旧格式引擎SimpleDateFormat
:
Calendar cal = new GregorianCalendar(2015, 3, 1, 17, 45);
SimpleDateFormat sf = new SimpleDateFormat("HH:mm a", Locale.US);
System.out.println(sf.format(cal.getTime())); // 17:45 PM
Java-8 (with the new package java.time.format
):
Java-8(带有新包java.time.format
):
LocalTime time = LocalTime.of(17, 59);
DateTimeFormatter tf = DateTimeFormatter.ofPattern("HH:mm a", Locale.US);
System.out.println(tf.format(time)); // 17:59 PM
Here we don't observe any overridein both cases. I prefer that approach because using the combination of "HH" and "a" is in my opinion rather an indication for a programming error (the user has obviously not thought enough about the meaning and official description of those pattern symbols).
在这里我们没有发现任何覆盖在这两种情况下。我更喜欢这种方法,因为在我看来,使用“HH”和“a”的组合而不是一个编程错误的指示(用户显然没有充分考虑这些模式符号的含义和官方描述)。
Now let us investigate the parsing mode(and we will use strict mode to observe what is really going behind the scene):
现在让我们研究解析模式(我们将使用严格模式来观察幕后的真实情况):
String dateString = "23 Dec 2015 1:4 PM";
SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy H:m a", Locale.US);
df.setLenient(false);
Date date = df.parse(dateString);
// java.text.ParseException: Unparseable date: "23 Dec 2015 1:4 PM"
How is the behaviour in Java-8? It is the same but with a clearer error message:
Java-8 中的行为如何?它是相同的,但有更清晰的错误信息:
DateTimeFormatter tf = DateTimeFormatter.ofPattern("H:m a", Locale.US);
tf = tf.withResolverStyle(ResolverStyle.STRICT);
LocalTime.parse("1:4 PM", tf);
// DateTimeException: Cross check failed: AmPmOfDay 0 vs AmPmOfDay 1
This message tells us that the parsed hour (in 24-hour format!) with value "1" indicates AM while the text input contains the other parsed AM/PM-value "PM". This ambivalence cannot be resolved.
此消息告诉我们,值为“1”的解析小时(以 24 小时制!)表示 AM,而文本输入包含其他解析的 AM/PM 值“PM”。这种矛盾无法解决。
Lesson: We have to be careful with lenient parsing where contradictious information can be ignored and lead to false assumptions.The accepted answer of @Arjun is completely wrong.
教训:我们必须小心宽松的解析,在这种情况下,矛盾的信息可能会被忽略并导致错误的假设。@Arjun 接受的答案是完全错误的。
By the way: Please use Locale.US
or new Locale("en", "US")
instead of new Locale("en-US")
because the language "en-US" does not exist.
顺便说一句:请使用Locale.US
或new Locale("en", "US")
代替,new Locale("en-US")
因为“en-US”语言不存在。