Android:我无法弄清楚 SimpleDateFormat
我已经尝试了很多次,但我无法让我的 RSS 应用程序将 pubDate 正确格式化为更用户友好的格式。
String str = "26/08/1994";
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); //please notice the capital M
Date date = formatter.parse(str);
该代码看起来很简单,但我在 formatter.parse(str) 上收到未处理的类型解析异常错误。一旦开始工作,我需要将我的 RSS Pubdate 转换为 MM/dd。
设置文本的代码行在这里:
listPubdate.setText(myRssFeed.getList().get(position).getPubdate());
我是否只需将其更改为:
listPubdate.setText(date);
这看起来很简单,以至于我找不到答案,这让我发疯。
I have tried and tried, but I cannot get my RSS app to properly format the pubDate into a more user friendly format.
String str = "26/08/1994";
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); //please notice the capital M
Date date = formatter.parse(str);
That code looks simple enough, but I get an unhandled type parse exception error on formatter.parse(str). Once that gets working, I then need to convert my RSS Pubdate to MM/dd.
The line of code to set the text for that is here:
listPubdate.setText(myRssFeed.getList().get(position).getPubdate());
Do I just change that to:
listPubdate.setText(date);
This looks so simple that it's driving me nuts that I can't find the answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过此获取日期
并希望采用简单的格式,
以便您可以非常轻松地使用它。
you can get the date by this
and want to put in simple format then
so you can use this very easily.
在我看来,您实际上正在运行这个并收到错误。正如其他人指出的那样,问题是您需要将 formatter.parse 调用包装在 try/catch 块中。这是一个编译问题,而不是运行时问题。
一旦解决了这个编译问题,您拥有的代码将按照您的预期工作。
使用第二个格式化程序来获取所需的 MM/dd 输出。
It sounds to me like you are actually running this and getting the error. As others have pointed out, the problem is you need to wrap the
formatter.parse
call in a try/catch block. This is a compilation problem, not a runtime problem.The code you have will work as you expect once you fix this compile problem.
Use a second formatter to get the MM/dd output you want.
为此,您需要显式处理异常,方法是声明当前执行的方法只是抛出异常,或者捕获它。有关更多信息,我强烈建议您阅读Java 教程中的异常课程 。
这是捕获异常的示例。
For that, you'll need to explicitly handle the exception, either by declaring that the currently executing method just throws it, or by catching it. For more information, I highly recommend going through the Exceptions Lesson in the Java Tutorial.
Here's an example of catching the exception.
SimpleDateformat.parse(String) 抛出一个已检查的异常...将其包装在 try/catch 块中。
SimpleDateformat.parse(String) throws a checked exception... wrap it in a try/catch block.
下面是示例代码...注意 SimpleDateFormat 构造函数中的格式。要解析日期的字符串的格式应与 SimpleDateFormat 构造函数中传递的字符串的格式类似
public Date getDate(String str) {
SimpleDateFormat sdFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss");
日期 d = null;
Below is the sample code ... pay attention to format inside SimpleDateFormat Constructor. This string which to be parsed for date should be similar in format to that of string passed in SimpleDateFormat constructor
public Date getDate(String str) {
SimpleDateFormat sdFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss");
Date d = null;