Android:我无法弄清楚 SimpleDateFormat

发布于 2024-11-15 11:34:35 字数 576 浏览 2 评论 0原文

我已经尝试了很多次,但我无法让我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

柒七 2024-11-22 11:34:35

您可以通过此获取日期

// get the current date
            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);

并希望采用简单的格式,

String date=String.format(mDay+"/"+mMonth+"/"+mYear);

以便您可以非常轻松地使用它。

you can get the date by this

// get the current date
            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);

and want to put in simple format then

String date=String.format(mDay+"/"+mMonth+"/"+mYear);

so you can use this very easily.

眼眸印温柔 2024-11-22 11:34:35

在我看来,您实际上正在运行这个并收到错误。正如其他人指出的那样,问题是您需要将 formatter.parse 调用包装在 try/catch 块中。这是一个编译问题,而不是运行时问题。

一旦解决了这个编译问题,您拥有的代码将按照您的预期工作。

使用第二个格式化程序来获取所需的 MM/dd 输出。

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

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.

    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();
    }
旧夏天 2024-11-22 11:34:35

我在 formatter.parse(str) 上收到未处理的类型解析异常错误

为此,您需要显式处理异常,方法是声明当前执行的方法只是抛出异常,或者捕获它。有关更多信息,我强烈建议您阅读Java 教程中的异常课程

这是捕获异常的示例。

String str = "26/08/1994";
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); //please notice the    capital M
Date date;
try
{
  date = formatter.parse(str);
}
catch (ParseException e)
{
  // Handle error condition.
}

I get an unhandled type parse exception error on formatter.parse(str)

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.

String str = "26/08/1994";
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); //please notice the    capital M
Date date;
try
{
  date = formatter.parse(str);
}
catch (ParseException e)
{
  // Handle error condition.
}
无人接听 2024-11-22 11:34:35

SimpleDateformat.parse(String) 抛出一个已检查的异常...将其包装在 try/catch 块中。

SimpleDateformat.parse(String) throws a checked exception... wrap it in a try/catch block.

冷血 2024-11-22 11:34:35

下面是示例代码...注意 SimpleDateFormat 构造函数中的格式。要解析日期的字符串的格式应与 SimpleDateFormat 构造函数中传递的字符串的格式类似

public Date getDate(String str) {
SimpleDateFormat sdFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss");
日期 d = null;

    try {

        String str1 = str.substring(0, str.lastIndexOf(" ")).substring(0,
                str.lastIndexOf(" "));
        String str2 = str1.substring(0, str1.lastIndexOf(" "));
        Log.v("str1", str2);

        d = sdFormat.parse(str2);
    } catch (java.text.ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return d;
}

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;

    try {

        String str1 = str.substring(0, str.lastIndexOf(" ")).substring(0,
                str.lastIndexOf(" "));
        String str2 = str1.substring(0, str1.lastIndexOf(" "));
        Log.v("str1", str2);

        d = sdFormat.parse(str2);
    } catch (java.text.ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return d;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文