Android 安卓:我出错了;无法将给定对象格式化为日期;从JSON响应转换日期时
这是我的密码。在尝试转换我的repsonse时,出现错误“无法将给定对象格式化为日期”Android 安卓:我出错了;无法将给定对象格式化为日期;从JSON响应转换日期时,android,date,simpledateformat,Android,Date,Simpledateformat,这是我的密码。在尝试转换我的repsonse时,出现错误“无法将给定对象格式化为日期” SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); holder.date.setText(sdf.format(item.getPrice().getDate())); 如何将我的销售代表从2017-02-03T11:44:52.6152Z转换为2017年2月3日? 谢谢试试这样的方法: SimpleDateForma
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
holder.date.setText(sdf.format(item.getPrice().getDate()));
如何将我的销售代表从2017-02-03T11:44:52.6152Z转换为2017年2月3日?
谢谢试试这样的方法:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date3 = null;
date3 = sdf.parse(item.getPrice().getDate());
formatter = new SimpleDateFormat("dd-MM-yyy");
String date1 = formatter.format(date3);
holder.date.setText(date1);
代码中有两个问题
Format
以Date
作为参数,您正在传递String
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
try{
Date a= sdf.parse(item.getPrice().getDate());
SimpleDateFormat f = new SimpleDateFormat("dd-MMMM-yyy");
holder.date.setText(f.format(a));
} catch (Exception e) {
e.printStackTrace();
System.out.print(e.toString());
}
}
使用以下代码将日期字符串转换为您的格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",Locale.getDefault());
//Date date3 = sdf.parse("2017-02-03T11:44:52.6152Z");
Date date3 = sdf.parse(item.getPrice().getDate());
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMMM-yyyy",Locale.getDefault());
String convertedDate= formatter.format(date3);
这是我从响应holder.date.setText(sdf.format(item.getPrice().getDate())获得的日期;回复是2017-02-03T11:44:52.6152Z。那么,如何使我的响应被转换呢?您是否尝试过上面的代码(使用您的特定案例更新)执行选项?你想解析的日期是什么?如果您得到的是:2017-02-03T11:44:52.6152Z,2017-02-03T11:44:52.6152Z是我的JSON响应。这是获取我的响应holder.date.setText(sdf.format(item.getPrice().getDate())的代码;