安卓:我想不通的SimpleDateFormat [英] Android: I can't figure out SimpleDateFormat

查看:103
本文介绍了安卓:我想不通的SimpleDateFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经试了又试,但我不能让我的RSS应用程序,以正确格式化pubdate的融入了更多人性化的格式。

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);

这code看起来很简单,但我得到的formatter.parse(STR)未处理的类型解析异常错误。一旦获得工作,然后我需要我的RSS pubdate的转换为MM / DD。

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.

的code线设置文本正在这里:

The line of code to set the text for that is here:

  listPubdate.setText(myRssFeed.getList().get(position).getPubdate());

我只是修改成:

  listPubdate.setText(date);

这看起来那么简单,它的驾驶我坚果,我无法找到答案。

This looks so simple that it's driving me nuts that I can't find the answer.

推荐答案

这听起来像你对我实际上的运行的这一点,并得到错误。正如其他人所指出的,问题是你需要用一个try / catch块的 formatter.parse 通话。这是一个的编译问题,而不是一个运行时的问题。

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.

在code,你必须将工作按预期一旦你解决这个编译问题。

The code you have will work as you expect once you fix this compile problem.

使用第二格式化得到你想要的MM / DD输出。

Use a second formatter to get the MM/dd output you want.

    String str = "26/08/1994";

    SimpleDateFormat inputFormatter = new SimpleDateFormat("dd/MM/yyyy"); //please notice the    capital M
    SimpleDateFormat outputFormatter = new SimpleDateFormat("MM/dd");

    try {
        Date date = inputFormatter.parse(str);
        String text = outputFormatter.format(date);
        listPubdate.setText(text);
    } catch (ParseException e ) {
        e.printStackTrace();
    }

这篇关于安卓:我想不通的SimpleDateFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆