从Android JSON提要转换UNIX时间

时间:2012-03-09 17:44:06

标签: android eclipse json unix datetime

我知道有类似的问题,但我找不到适用于我的解决方案。我添加了用于调用JSON提要的代码并将其显示在ListAdapter中。我需要让我的字段“ShowDate”和“ShowStart”以可读格式显示,而不是UNIX。

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;

@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("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);

        }

    });
}

任何建议或建议将不胜感激。我花了太多时间在这上面,因为我认为这很简单。谢谢!

4 个答案:

答案 0 :(得分:2)

感谢所有回复。事实证明,这就是我需要做的一切。它可能只是一个快速修复,但它的工作原理。我需要将它放在我的JSONArray中。

String showDate = e.getString("ShowDate");
long epoch = Long.parseLong( showDate );
Date showDatePresent = new Date( epoch * 1000 );
SimpleDateFormat sdf = new SimpleDateFormat("E, MMMM d");
String dateOfShow = sdf.format(showDatePresent);
map.put("showdate", "" + dateOfShow);

答案 1 :(得分:0)

检查出SimpleDateFormat对象。

答案 2 :(得分:0)

您可以使用SimpleDateFormatparse您提供的日期,然后format以您希望的方式使用

http://developer.android.com/reference/java/text/SimpleDateFormat.html

答案 3 :(得分:0)

与其他人一样,要将时间戳转换为使用SimpleDateFormat的日期。

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

但似乎你不知道该把它放在哪里。您需要在ViewBinder对象中设置SimpleAdapter,以您希望的格式显示日期。

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

adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        if (columnIndex == cursor.getColumnIndex("showdate")
                || columnIndex == cursor.getColumnIndex("showstart")) {
            Date d = new Date(cursor.getLong(columnIndex));
            String formattedDate = new SimpleDateFormat("mm/dd/yy hh:mm:ss").format(d);
            ((TextView) view).setText(formattedDate);

            return true;
        }
        return false;
    }
});

setListAdapter(adapter);