1

有人可以帮我在我的脚本中将时间戳转换为日期。我有一个列表视图,我的列表视图在今天、昨天、前天和昨天加载。前几天我只有时间戳代码?

这是我的主要课程:

            if(_hasConnection()){

            dataList.clear();

            System.out.println("Start loading JSON");
             JSONArray json = JSONFunctions.getJSONfromURL(Config.DOMAIN + "/report.php?category="+getCategory()+"&image=" + Config.IMAGE_WIDTH + "x" + Config.IMAGE_HEIGHT + "&id=1&date=1&limit=" + Config.LIMIT);
            System.out.println("End loading JSON");

            for (int i = 0; i < json.length(); i++) {
                try {
                    JSONObject row = json.getJSONObject(i);

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

                    map.put("id", row.getString("id"));
                    map.put("title", row.getString("title"));
                    map.put("image", row.getString("image"));
                    map.put("date", row.getString("date"));
                 dataList.add(map); 

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }



            System.out.println("End putting in dataList");

            adapter.reset();

            int continueI = 0;
            for (int i = 0; i < dataList.size(); i++) {
                if(i > continueI) {
                    int daysAgo = Utils.daysAgo(dataList.get(i).get("date"));
                    //System.out.println("T: " + dataList.get(i).get("title") + " is daysAgo: " + daysAgo);

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

                    for (int q = 0; q < dataList.size(); q++) {
                        int daysAgoCompare = Utils.daysAgo(dataList.get(q).get("date"));

                        if(daysAgo == daysAgoCompare) {
                            objects.add(dataList.get(q));
                            continueI = q;
                        }
                    }

                    if(objects.size() > 0) {
                        String dayNaming;
                        if(daysAgo == 0) {
                            dayNaming = getString(R.string.today);
                        }else if(daysAgo == 1) {
                            dayNaming = getString(R.string.yesterday);
                        }
                        else if(daysAgo == 2) {
                            dayNaming = getString(R.string.day_before_yesterday);
                        }
                        else
                        {
                            //SimpleDateFormat dateformatddMMMM = new SimpleDateFormat("dd/MM/yyyy");
                            dayNaming = String.valueOf(dataList.get(i).get("date")); // new StringBuilder( dateformatddMMMM.format( new Date(Long.parseLong() * 10) ) ).toString();
                        }

                        //System.out.println("T: " + dayNaming + " is added");

                        adapter.addSection(dayNaming, new LazyAdapter(getApplicationContext(), Main.this, objects));

                        //adapter.addSection(dayNaming, new ArrayAdapter<String>(Main.this, R.layout.list_item, objects));  
                    }
4

1 回答 1

1

很简单,您可以将时间戳字符串更改为日期,如下所示,

long timeStamp = Long.parseLong("your_timestamp_value");
Date date = new Date(timeStamp);
于 2012-11-26T11:52:00.353 回答