原因:java.lang.IllegalArgumentException:不好 class:class java.lang.String

Caused by: java.lang.IllegalArgumentException: Bad class: class java.lang.String

我正在尝试格式化从数据库 table 检索到的日期,我想将“2015 02 11 12 00”格式化为 "Feb 2",但在我格式化时出现错误它。还有其他格式化这些日期的方法吗?

遵循代码:

protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_events, "GET", params);

        // Check your log cat for JSON reponse
        Log.d("All events: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // events found
                // Getting Array of events
                events = json.getJSONArray(TAG_EVENTS);

                // looping through All events
                for (int i = 0; i < events.length(); i++) {
                    JSONObject c = events.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_PID);
                    String eventname = c.getString(TAG_EVENTNAME);
                    String eventstart = c.getString(TAG_EVENTSTART);

                  String eventstartconvert = formatdate.format(eventstart); //this line gives an error.


                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_PID, id);
                    map.put(TAG_EVENTNAME, eventname);
                    map.put(TAG_EVENTSTART,eventstartconvert);

                    // adding HashList to ArrayList
                    eventsList.add(map);
                }
            }

这是我的 logcat:

02-11 09:14:40.997  25966-26198/com.example.jithea.testlogin E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask.done(AsyncTask.java:299)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
        at java.util.concurrent.FutureTask.run(FutureTask.java:239)
        at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:838)
 Caused by: java.lang.IllegalArgumentException: Bad class: class java.lang.String
        at java.text.DateFormat.format(DateFormat.java:359)
        at java.text.Format.format(Format.java:93)
        at com.example.jithea.testlogin.EventsActivity$LoadAllevents.doInBackground(EventsActivity.java:137)
        at com.example.jithea.testlogin.EventsActivity$LoadAllevents.doInBackground(EventsActivity.java:91)
        at android.os.AsyncTask.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask.run(FutureTask.java:234)

在 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 在 java.lang.Thread.run(Thread.java:838)

DateFormat.format expects Number or Date but you are passing it a String. You may want to parse the string 但对其进行格式化只会生成与解析相同的字符串。

你能把这个格式用于日期格式化吗......

DateFormat inputFormatter1 = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = inputFormatter1.parse("2015-2-22");

DateFormat outputFormatter1 = new SimpleDateFormat("dd-MMM-yyyy");
String output1 = outputFormatter1.format(date1); // 

// 如果在此格式中输入为 2015-2-22,则输出为 22-Feb-2015

你应该格式化输入然后传递给输出格式

try {
    String formattedString = new SimpleDateFormat("dd MMM yyyy").format(new SimpleDateFormat("yyyy-MM-dd").parse("2014-07-31"));
} catch (ParseException e) {
    e.printStackTrace();
}