简体   繁体   English

Android:我不知道SimpleDateFormat

[英]Android: I can't figure out SimpleDateFormat

I have tried and tried, but I cannot get my RSS app to properly format the pubDate into a more user friendly format. 我已经尝试了很多次,但是我无法获得RSS应用程序以将pubDate正确格式化为更加用户友好的格式。

    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). 该代码看起来很简单,但是我在formatter.parse(str)上收到未处理的类型解析异常错误。 Once that gets working, I then need to convert my RSS Pubdate to MM/dd. 一旦开始工作,我就需要将RSS发布日期转换为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. 这看起来很简单,以至于我发疯了,以至于找不到答案。

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. 因此您可以非常轻松地使用它。

I get an unhandled type parse exception error on formatter.parse(str) 我在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 . 有关更多信息,我强烈建议您阅读Java教程中的“异常”课程

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.
}

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. 正如其他人指出的那样,问题在于您需要将formatter.parse调用包装在try / catch块中。 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. 使用第二个格式化程序获取所需的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();
    }

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

Below is the sample code ... pay attention to format inside SimpleDateFormat Constructor. 下面是示例代码...请注意SimpleDateFormat构造函数中的格式。 This string which to be parsed for date should be similar in format to that of string passed in SimpleDateFormat constructor 此日期要解析的字符串的格式应类似于在SimpleDateFormat构造函数中传递的字符串的格式

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM