我想按降序排序日期。 日期来自json。我通过AsyncTask解析它。但我不知道如何按降序排序日期。这是我的代码:
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(EventsActivity.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... urls) {
try {
//------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
Log.d("EventsActvity:data from server",data);
JSONArray array = new JSONArray(data);
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
Events event = new Events();
event.setevent_title(obj.getString("event_title"));
event.setstart_date(obj.getString("start_date"));
event.setsmall_picture_path(obj.getString("small_picture_path"));
event.setend_date(obj.getString("end_date"));
event.setstart_time(obj.getString("start_time"));
event.setend_time(obj.getString("end_time"));
event.setlocation(obj.getString("location"));
event.setevent_desc(obj.getString("event_desc"));
event.setpicture_path(obj.getString("picture_path"));
event.setrequire_registration(obj.getString("require_registration"));
event.setevent_id(obj.getString("event_id"));
event.setquantity_left(obj.getString("quantity_left"));
eventList.add(event);
}
return true;
}
else
{
return false;
}
//------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
//return false;
} catch (IOException e) {
e.printStackTrace();
//return false;
} catch (JSONException e) {
e.printStackTrace();
//return false;
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
adapter.notifyDataSetChanged();
if(result == false)
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
如何按降序对日期进行排序?
答案 0 :(得分:0)
您的Event类必须实现Comparable
接口。然后,覆盖compareTo()
方法以按日期排序。最后,使用Collections.sort(list)
对您的列表进行排序。
答案 1 :(得分:0)
这是一个简单的溶胶, 它将日期字符串转换为时间(以毫秒为单位),并将它们作为Long值进行比较 它将基于将日期与列表中顶部偶数对象的日期进行比较来插入事件对象
我使用 startDate ,您可以更改要使用的字段
我使用Vector()
,所以我可以在特定索引
您已经拥有的代码的这些更改,它解析Json的位置以及在事件对象中设置值:
Vector<Events> events = new Vector<Events>();
try {
// System.out.println(sdf.parse("2014-08-17 14:55:22").getTime());
// System.out.println(sdf.parse("2014-08-17 14:55:23").getTime());
// System.out.println(sdf.parse("2014-08-17 14:55:24").getTime());
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
//parse and init the event bean ...
//replace eventList.add(event); with:
if(eventList.size() == 0){
//1st item, add it on loc=0
eventList.add(event);
}else{
long time = getTimeLong(event.getstart_date);
if(time > getTimeLong(events.get(eventList.size()-1).getStartDate())){
eventList.add(0,event);//add event at top of list
}else{
eventList.add(event);//add event at end of list
}
}
}//for loop
} catch (ParseException e) {
e.printStackTrace();
}
此方法的时间为毫秒
private long getTimeLong(String dateTime){
if(dateTime.trim().equals("")){
return -1;
}
try {
return new SimpleDateFormat("yyyy-MM-d H:mm:ss").parse(dateTime).getTime();
} catch (ParseException e) {
e.printStackTrace();
return -1;
}
}
您还可以在将字符串转换为日期after(Date when)
before(Date when)
或getTime()
在Date对象而不是date1.before(date2)
比较日期