如何解析json响应中的日期并在textview中设置。
我的Json回答:
{“创造物”:“2016-03-14:00:01”}
我在我的Textview中想要的:
下午4:00 03/14
我的listview适配器:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
public class ListViewAdapter extends BaseAdapter {
Context ctx;
ArrayList<HashMap<String, String>> arraylist;
LayoutInflater inflater;
TextView tvPlaceName, tvTime;
String longitude, latitude;
String out;
ListViewAdapter(Context ctx, ArrayList<HashMap<String, String>> arraylist) {
this.ctx = ctx;
this.arraylist = arraylist;
}
@Override
public int getCount() {
return arraylist.size();
}
@Override
public Object getItem(int position) {
return arraylist.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listitem, parent, false);
tvPlaceName = (TextView) itemView.findViewById(R.id.tvPlaceName);
tvTime = (TextView) itemView.findViewById(R.id.tvTime);
// Log.d("uname", arraylist.get(position).get("Username"));
tvPlaceName.setText(arraylist.get(position).get("alert_message"));
tvTime.setText(arraylist.get(position).get("createdate"));
longitude = arraylist.get(position).get("longitude");
latitude = arraylist.get(position).get("latitude");
return itemView;
}
private String convertTime(String time) {
SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
SimpleDateFormat format1 = new SimpleDateFormat("hh:mm aa MM-dd");
java.util.Date date = null;
try {
date = format.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
String convertedDate = format1.format(date);
return convertedDate;
}
}我已经取得了我的反应,但我不知道如何形成一个特定的日期。
我们衷心欢迎各位提出建议。
发布于 2016-03-15 05:13:23
就像这样
tvTime.setText(convertTime(arraylist.get(position).get("createdate")));使用以下代码
private String convertTime(String time) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat format1 = new SimpleDateFormat("hh:mm aa MM-dd");
java.util.Date date = null;
try {
date = format.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
String convertedDate = format1.format(date);
return convertedDate;
}发布于 2016-03-15 05:01:04
使用以下代码
下面代码返回日历对象
public static Calendar StringDateConverter(String date) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
return dateToCalendar(df.parse(date.toUpperCase()));
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
private static Calendar dateToCalendar(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;
}使用此代码将日历事件转换为字符串。
public static String ConvertDateToDate(Calendar calendar) {
DateFormat df = new SimpleDateFormat("HH:mmaa MM/dd", Locale.getDefault());
return df.format(calendar.getTime());
}发布于 2016-03-15 05:31:35
public String convertDateFormat() {
String formatedDate ="";
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat outputFormat = new SimpleDateFormat("HH:mmaa MM/dd");
Date date = null;
try {
date = inputFormat.parse(parseJsonAndGetCreateDateString());
formatedDate = outputFormat.format(date);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
return formatedDate;
}
public String parseJsonAndGetCreateDateString(){
String response = "{\"createdate\":\"2016-03-14 04:00:01\"}";
String createDate = "";
try{
JSONObject obj = new JSONObject(response);
createDate = obj.getString("createdate");
}catch(Exception e){
e.printStackTrace();
}
return createDate;
}https://stackoverflow.com/questions/36002939
复制相似问题