Android JSON time conversion from Unix

白昼怎懂夜的黑 提交于 2019-12-25 08:53:10

问题


I asked this question earlier and got some great feedback, but I still wasn't able to get my program running correctly. The Basics: Its a scheduling app. I am trying to get a field in my JSON feed to be converted into a human readable time/date and display in my ListView adaptor. In the code below I am pulling in a string called "ShowDate" that contains the Unix timestamp. Right now (before I added SimpleDateFormat) the string displays the Unix code. When I added the SimpleDateFormat my activity crashes on load. I think I just have something mis-labeled or in the wrong position. Any help is appreciated. I've included the code for my activity and the logcat error I receive.

public class Schedule extends ListActivity {

protected EditText searchText;
protected SQLiteDatabase db;
protected Cursor cursor;
protected TextView textView;
protected ImageView imageView;
protected ArrayList<HashMap<String, String>> myList;

Date F = new Date("showdate"); String formattedDate = new SimpleDateFormat("mm/dd/yy hh:mm:ss").format(F);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.schedule);



    View layout = findViewById(R.id.gradiant);

    GradientDrawable gd = new GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM, new int[] {
                    0xFF95B9C7, 0xFF06357A });
    gd.setCornerRadius(2f);

    layout.setBackgroundDrawable(gd);

    myList = new ArrayList<HashMap<String, String>>();

    JSONObject jsonObjSend = new JSONObject();
    JSONObject json = JSONfunctions
            .getJSONfromURL("myAPIURL");


        JSONArray current = json.getJSONArray("d");


        for (int i = 0; i < current.length(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            JSONObject e = current.getJSONObject(i);

            map.put("id", String.valueOf(i));
            map.put("showid", "" + Html.fromHtml(e.getString("ShowID")));
            map.put("name", "" + Html.fromHtml(e.getString("Title")));
            map.put("showvenue", "" + e.getString("ShowVenue"));
            map.put("subtutle", "" + e.getString("SubTitle"));
            map.put("venueid", "" + e.getString("VenueID"));
            map.put("showdate", "" + e.getString("ShowDate"));
            map.put("showstart", "" + e.getString("ShowStart"));
            map.put("showend", "" + e.getString("ShowEnd"));
            map.put("gatesopen", "" + e.getString("GatesOpen"));
            map.put("aboutartist", "" + e.getString("AboutArtist"));
            map.put("program", "" + Html.fromHtml(e.getString("Program")));
            map.put("programnotes", "" + Html.fromHtml(e.getString("ProgramNotes")));
            map.put("pricing", "" + e.getString("Pricing"));
            map.put("featuring", "" + e.getString("Featuring"));
            map.put("parkdetails", "" + e.getString("ParkDetails"));
            map.put("starred", "" + e.getString("Starred"));
            map.put("image500", "" + e.getString("Image500"));
            map.put("image250", "" + e.getString("Image250"));
            map.put("aboutartist", "" + Html.fromHtml(e.getString("AboutArtist")));


            myList.add(map);
        }
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());

    }



    ListAdapter adapter = new SimpleAdapter(this, myList,R.layout.line_item, 
            new String[] { "name", "showdate","showstart", "showvenue", "image250" }, 
            new int[] { R.id.title, R.id.showdate,R.id.showstart,R.id.showvenue, R.id.list_image });

    setListAdapter(adapter);

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);
    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            HashMap<String, String> hashMap = myList.get(position);
            // hashMap.put("map", hashMap);
            Intent intent = new Intent(getApplicationContext(),
                    ArtistDetails.class);
            intent.putExtra("map", hashMap);
            startActivity(intent);

        }

    });
}

LOGS:

 03-09 13:36:29.271: D/dalvikvm(3239): GC_EXTERNAL_ALLOC freed 61K, 53% free 2578K/5379K, external 1625K/2137K, paused 44ms
03-09 13:36:31.191: D/dalvikvm(3239): GC_EXTERNAL_ALLOC freed 5K, 52% free 2597K/5379K, external 2881K/3528K, paused 48ms
03-09 13:36:31.571: D/dalvikvm(3239): GC_EXTERNAL_ALLOC freed 2K, 52% free 2597K/5379K, external 4811K/6009K, paused 36ms
03-09 13:36:31.892: D/dalvikvm(3239): GC_EXTERNAL_ALLOC freed <1K, 52% free 2599K/5379K, external 6741K/8419K, paused 42ms
03-09 13:36:32.601: D/dalvikvm(3239): GC_EXTERNAL_ALLOC freed <1K, 52% free 2603K/5379K, external 10601K/10719K, paused 36ms
03-09 13:36:35.951: D/AndroidRuntime(3239): Shutting down VM
03-09 13:36:35.951: W/dalvikvm(3239): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-09 13:36:35.972: E/AndroidRuntime(3239): FATAL EXCEPTION: main
03-09 13:36:35.972: E/AndroidRuntime(3239): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.aqdct.ravinia2012v11/com.aqdct.ravinia2012v11.Schedule}: java.lang.IllegalArgumentException
03-09 13:36:35.972: E/AndroidRuntime(3239):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1544)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at android.os.Looper.loop(Looper.java:123)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at android.app.ActivityThread.main(ActivityThread.java:3647)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at java.lang.reflect.Method.invokeNative(Native Method)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at java.lang.reflect.Method.invoke(Method.java:507)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at dalvik.system.NativeStart.main(Native Method)
03-09 13:36:35.972: E/AndroidRuntime(3239): Caused by: java.lang.IllegalArgumentException
03-09 13:36:35.972: E/AndroidRuntime(3239):     at java.util.Date.parse(Date.java:505)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at java.util.Date.<init>(Date.java:148)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at com.aqdct.ravinia2012v11.Schedule.<init>(Schedule.java:39)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at java.lang.Class.newInstanceImpl(Native Method)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at java.lang.Class.newInstance(Class.java:1409)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1536)
03-09 13:36:35.972: E/AndroidRuntime(3239):     ... 11 more
03-09 13:36:39.331: I/Process(3239): Sending signal. PID: 3239 SIG: 9

Please let me know if there is any other information needed to help get this working.


回答1:


Date(string) is depreciated.

Instead you should use DateFormat.parse(String s)

That aside, why are you passing "showdate" to the Date constructor below? You'd want to pass in some actual time otherwise it's not going to parse anything.

Date F = new Date("showdate"); String formattedDate = new SimpleDateFormat("mm/dd/yy hh:mm:ss").format(F);


来源:https://stackoverflow.com/questions/9640063/android-json-time-conversion-from-unix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!