Java 无法使用 AM/PM 标记解析日期时间字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3618676/
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
Unable to parse DateTime-string with AM/PM marker
提问by aksamit
The string I want to format looks like this: String datetime = "9/1/10 11:34:35 AM"
我要格式化的字符串如下所示: String datetime = "9/1/10 11:34:35 AM"
Following pattern for SimpleDateFormat works:
SimpleDateFormat 的以下模式有效:
SimpleDateFormat sdf = SimpleDateFormat("M/d/yy h:mm:ss");
Date d = sdf.parse(datetime);
System.out.println(d);
Output> [Wed Sep 01 11:34:35 CEST 2010]
However I need to parse the AM/PM marker as well, and when I add that to the pattern I receive an exception.
但是,我还需要解析 AM/PM 标记,当我将它添加到模式时,我收到一个异常。
Pattern that doesn't work:
不起作用的模式:
SimpleDateFormat sdf = SimpleDateFormat("M/d/yy h:mm:ss a");
I have tried with this also with same exception:
我也试过这个,但有同样的例外:
SimpleDateFormat sdf = SimpleDateFormat("M/d/yy h:mm:ss aa");
Exception:
例外:
java.text.ParseException: Unparseable date: "9/1/10 11:34:35 AM"
I have looked through the API at http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html#textbut can?t seem to find where I do wrong.
我已经浏览了http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html#text 上的 API,但似乎找不到我做错的地方。
Any suggestions?
有什么建议?
采纳答案by Moritz
One possibility is that your default Locale
has different symbols for AM/PM. When constructing a date format you should always supply a Locale
unless you really want to use the system's default Locale
, e.g.:
一种可能性是您的默认设置Locale
有不同的 AM/PM 符号。在构建日期格式时,Locale
除非您真的想使用系统的默认值Locale
,否则您应该始终提供 a ,例如:
SimpleDateFormat sdf = new SimpleDateFormat("M/d/yy h:mm:ss a", Locale.US)
回答by Marek Tuchalski
If you are working with FreeMarker for Java and pop on this issue use below code. I had this problem, my locale set AM/PM as DE. Not sure why...
<#setting locale="en_US">
如果您正在使用 FreeMarker for Java 并解决此问题,请使用以下代码。我遇到了这个问题,我的语言环境将 AM/PM 设置为 DE。不知道为什么...
<#setting locale="en_US">
回答by droidd
I am taking an example of date given below and print the formatted date into 24-hour format if suits your requirement.
我以下面给出的日期为例,如果符合您的要求,将格式化的日期打印为 24 小时格式。
String inputdate="9/1/10 11:34:35 AM";
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd/MM/yy hh:mm:ss aa",Locale.getDefault());
try {
System.out.println(""+new SimpleDateFormat("dd/MM/yy HH:mm:ss",Locale.getDefault()).format(simpleDateFormat.parse(inputdate)));
} catch (ParseException e) {
e.printStackTrace();
}
If you still have any query, Please respond. Thanks.
如果您还有任何疑问,请回复。谢谢。
回答by Ole V.V.
Modern answer:
现代答案:
String datetime = "9/1/10 11:34:35 AM";
LocalDateTime dt = LocalDateTime.parse(datetime,
DateTimeFormatter.ofPattern("M/d/yy h:mm:ss a", Locale.ENGLISH));
This produces a LocalDateTime
of 2010-09-01T11:34:35. Beware of two digit years, though; DateTimeFormatter
will assume 2000 through 2099. For my birthday this would have been incorrect.
这产生了LocalDateTime
2010-09-01T11:34:35。不过要注意两位数的年份;DateTimeFormatter
将假设 2000 年到 2099 年。对于我的生日,这将是不正确的。
We still need to provide the locale. Since AM/PM markers are hardly used in practice in other locales than English, I considered Locale.ENGLISH
a fairly safe bet. Please substitute your own.
我们仍然需要提供语言环境。由于 AM/PM 标记在英语以外的其他语言环境中几乎不使用,我认为Locale.ENGLISH
这是一个相当安全的赌注。请用自己的代替。
The other answers were fine answers in 2010 and 2011. Already in 2014 the above was valid and I would have preferred it.
其他答案在 2010 年和 2011 年都是很好的答案。在 2014 年,上述内容已经有效,我会更喜欢它。