2015-02-11 62 views
3

我想格式化日期,我從我的數據庫表中檢索,我想格式從「2015 02 11 12 00」到「 2月2日「,但它格式化時會出錯。有沒有其他的方式來格式化這些日期?導致:java.lang.IllegalArgumentException:錯誤的類:類java.lang.String

繼代碼:

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$3.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$1.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$2.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) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573) 在java.lang.Thread.run(Thread.java:838)

+1

請張貼異常的完整堆棧跟蹤,使我們知道要啓動的線看。 – emerssso 2015-02-11 01:25:28

回答

12

你可能在你這個格式,日期格式化.......

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日比出認沽將是2月22日 - 2015年

+0

您可以根據您的設置格式要求.... like(「yyyy-MM-dd hh:mm:ss」) – 2015-02-11 02:57:55

+0

它的工作原理!謝謝.. – 2015-02-11 03:36:42

5

DateFormat.format預計NumberDate但你傳遞一個String。你可能想parse the string,但格式化它只會產生你將解析的相同的字符串。

+0

'錯誤:(138,31)錯誤:找不到格式合適的方法(SimpleDateFormat,String)'按照我聲明的方式'SimpleDateFormat formatdate = new SimpleDateFormat(「Mmm dd」);' – 2015-02-11 01:33:37

1

您應該格式的輸入,然後傳遞到輸出格式

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