java 使用 DateFormat.parse() 无法解析的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15505658/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:50:21  来源:igfitidea点击:

Unparseable date using DateFormat.parse()

javadatetimesimpledateformataem

提问by justacoder

I followed a few other solutions on this site to this dilemma, and I do not have Joda Time installed, but I'm still at a loss as to why this is failing.

我在这个网站上遵循了一些其他解决方案来解决这个难题,我没有安装 Joda Time,但我仍然不知道为什么会失败。

I also tried removing the colons, as one solution stated, but that did not help.

正如一种解决方案所述,我也尝试删除冒号,但这没有帮助。

currentNode.getProperty("jcr:created").getString()= 2013-03-07T11:57:08.596-05:00

currentNode.getProperty("jcr:created").getString()= 2013-03-07T11:57:08.596-05:00

I get this error: java.text.ParseException: Unparseable date: "2013-03-07T11:57:08.596-05:00"

我收到此错误:java.text.ParseException:无法解析的日期:“2013-03-07T11:57:08.596-05:00”

<%@page import="
    java.util.Date,
    java.text.SimpleDateFormat,
    java.text.DateFormat"
%>
<%
    DateFormat outputFormat = new SimpleDateFormat("MMMM dd, yyyy");
    DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    String currentDate = currentNode.getProperty("jcr:created").getString();
    Date date = inputFormat.parse(currentDate); // <-- Failing here
    String currentDateString = outputFormat.format(date);
%>

回答by drunken bot

The time zone formated as Zshould be -0500, not -05:00.

时区格式Z应为-0500,而不是-05:00

So I'd suggest you to replace

所以我建议你更换

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

with

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");

See SimpleDateFormat's javadocfor more details on available formats.

有关可用格式的更多详细信息,请参阅SimpleDateFormat 的 javadoc

If your jdk doesn't allow the X pattern, you'll have to fix the input string to remove the :. This can be done with a regex :

如果您的 jdk 不允许 X 模式,则必须修复输入字符串以删除:. 这可以通过正则表达式完成:

currentDate = currentDate.replaceAll(":(\d\d)$", "")

回答by Mies44

After testing the solution by drunken bot, I see that a timezone with half hours does not work, like -0530 (India).

通过醉酒机器人测试解决方案后,我发现半小时的时区不起作用,例如 -0530 (India).

So the improved answer therefore is:

因此,改进的答案是:

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

Note the added XXat the end, now also minutes are taken into account.

注意最后添加XX的,现在也考虑了分钟。

回答by rakhi4110

Though this question has been answered, there is an alternative way to achieve the solution, if your requirement is only to display the date of creation in the format "MMMM dd,yyyy".

尽管此问题已得到解答,但如果您的要求只是以“MMMM dd,yyyy”格式显示创建日期,则还有一种替代方法可以实现该解决方案。

There is a getDate() method for Property object which returns a Calendar object, from which can get date object using getTime().

Property 对象有一个 getDate() 方法,它返回一个 Calendar 对象,可以使用 getTime() 从中获取日期对象。

So, the above piece of code, would work if re-written as shown below.

因此,如果按如下所示重新编写,上面的代码将起作用。

<%@ page import="java.util.Calendar,
    java.text.SimpleDateFormat,
    java.text.DateFormat" %>
<%
DateFormat outputFormat = new SimpleDateFormat("MMMM dd, yyyy");
Calendar currentDate = currentNode.getProperty("jcr:created").getDate();
String currentDateString = outputFormat.format(currentDate.getTime()); %>

Thus, it would eliminate the need of converting the String to Date and then performing the remaining operations. Hope this helps.

因此,它将消除将字符串转换为日期然后执行剩余操作的需要。希望这可以帮助。