如何从json响应中解析日期并在textview中设置。
我的Json回应:
{"createdate":"2016-03-14 04:00:01"}
我想要在Textview中显示的内容:
4:00PM 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> arraylist;
LayoutInflater inflater;
TextView tvPlaceName, tvTime;
String longitude, latitude;
String out;
ListViewAdapter(Context ctx, ArrayList> 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;
}
}
我已经取得了回应,但是我不知道如何以特定的方式安排约会。
衷心欢迎建议
将日期字符串解析为某些Java对象的可能副本
检查我编辑的问题..如何格式化日期时间并在textview中设置
像这样
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;
}
我有listview ..我能够获取响应..但是如何在textview中设置它呢?
只需在您的适配器中添加以上功能...并在将名称设置为文本时使用此功能
您可以编辑答案并显示给我吗? z
别人告诉我,你是如何设定的名字TextView的,这样我会告诉你如何使用上述功能吧。
tvTime.setText(arraylist.get(position).get(" createdate")));
CREATEDATE是日期时间响应的名字
接受并喜欢它是否起作用。
检查我的更新quesiotn。我已经输入代码了。你现在可以告诉我吗
看到我更新的答案...。接受并喜欢它是否有效
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;
}
使用以下代码
下面的代码返回日历对象
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());
}
dateToCalendar =无法解析方法..
@tiger请检查更新的内容
我有listview ..我能够获取响应..但是如何在textview中设置它呢?
准确显示您的结果
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();
}
尝试这个:
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"的输出
我有listview ..我能够获取响应..但是如何在textview中设置它呢?
那么如果你是使用recyclerview你可以做这样的事情holder.tv_neme.setText(出);
@tiger:这是你想要的
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";