时间戳转换为日期android

时间:2012-11-26 11:23:48

标签: android unix-timestamp

有人可以帮助我在我的脚本中将时间戳转换为日期。我有一个listview,我的listview今天,昨天,前一天和昨天正确加载。我才得到时间戳代码的前几天?

这是我的主要课程:

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

1 个答案:

答案 0 :(得分:1)

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

long timeStamp = Long.parseLong("your_timestamp_value");
Date date = new Date(timeStamp);