android 解析json 日期格式,关于android:从Json Response更改日期和时间时间格式

如何从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";

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值