如何从json响应中解析日期并在textview中设置。
我的Json回复:
{" createdate":" 2016-03-14 04:00:01"}
我在Textview中想要的内容:
2014年3月4日下午4点
我的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;
}
}
我已经得到了回复,但我不知道如何以特定的方式形成约会。
欢迎提出建议
答案 0 :(得分:3)
这样做
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;
}
答案 1 :(得分:2)
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;
}
答案 2 :(得分:1)
使用以下代码
代码下方返回日历对象
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());
}
答案 3 :(得分:1)
private String getFormate(String date) throws ParseException {
Date d = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.ENGLISH).parse(date);
Log.d("Date", String.valueOf(d));
Calendar cal = Calendar.getInstance();
cal.setTime(d);
String monthName = new SimpleDateFormat("hh:mmaa MM/yyyy").format(cal.getTime());
return monthName;
}
并使用它来获取您的格式
try {
String myFormate = getFormate(d);
Log.d("date", myFormate);
} catch (ParseException e) {
e.printStackTrace();
}
此处 d 为String d = "2016-03-14 04:00:01";
答案 4 :(得分:1)
试试这个:
SimpleDateFormat dateFormat2 = new SimpleDateFormat("hh:mm:ss a MM/dd");
SimpleDateFormat hh_mm_ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date dat1 =hh_mm_ss.parse(arraylist.get(position).get("createdate"));
String out = dateFormat2.format(dat1);
tvTime.setText(out);
} catch (ParseException e) {
e.printStackTrace();
}
它将转换您的日期格式,并为您输出“时间04:00:01 AM 03/14”
答案 5 :(得分:1)
exact your result
SimpleDateFormat dateFormat2 = new SimpleDateFormat("hh:mma MM/dd");
SimpleDateFormat hh_mm_ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date dat1 = hh_mm_ss.parse("2016-03-14 04:00:01");
String out = dateFormat2.format(dat1);
System.out.println(out);
} catch (ParseException e) {
e.printStackTrace();
}