Java SimpleDateFormat解析问题
我正在尝试使用 SimpleDateFormat 类解析从 Java 网站获取的日期字符串,但出现了问题,而且我不知道为什么。
日期字符串采用以下语法:
"13:37 - Tue 28-Jun-2011"
所以我尝试执行以下操作:
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm - EEE dd-MMM-yyyy");
ParsePosition pos = new ParsePosition(0);
Date d = dateFormat.parse("13:37 - Tue 28-Jun-2011", pos);
正如我之前所说,这是行不通的;当我打印时
System.out.println(pos.getErrorIndex());
,它打印“8”,我认为这意味着错误位于 EEE 部分附近。我尝试过不同的排列,但没有任何效果。我做错了什么?
谢谢邦普夫
I'm trying to parse a date string I got out of a website in Java using the SimpleDateFormat class, but something goes wrong and I can't figure out why.
The date strings come in the following syntax:
"13:37 - Tue 28-Jun-2011"
So I tried doing the following:
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm - EEE dd-MMM-yyyy");
ParsePosition pos = new ParsePosition(0);
Date d = dateFormat.parse("13:37 - Tue 28-Jun-2011", pos);
As I said before, this doesn't work; when I print
System.out.println(pos.getErrorIndex());
it prints "8", which I assume means that the error is somewhere around the EEE part. I've tried different permutations but nothing worked. What am I doing wrong?
Thanks
bompf
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您尝试解析日期,这将起作用。我不知道你想用 ParsePosition 做什么
If your trying to parse the date this will work. I dont know what you are trying to do with
ParsePosition
它对我来说效果很好......
输出 -
It works fine for me...
Output -
我发现问题:我不知道我必须为日期格式设置区域设置。
现在可以了!
I found the problem: I did not know I have to set a locale for the date format..
This works now!