Android :从 JSON 响应转换日期时出现错误 "Cannot format given Object as a Date"
Android : I got Error "Cannot format given Object as a Date" when converting date from JSON response
这是我的代码。尝试转换我的回复时出现错误 "Cannot format given Object as a Date"。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
holder.date.setText(sdf.format(item.getPrice().getDate()));
如何将我的回复 2017-02-03T11:44:52.6152Z
转换为 03-February-2017?
谢谢
尝试这样的事情:
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);
这是我的代码。尝试转换我的回复时出现错误 "Cannot format given Object as a Date"。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
holder.date.setText(sdf.format(item.getPrice().getDate()));
如何将我的回复 2017-02-03T11:44:52.6152Z
转换为 03-February-2017?
谢谢
尝试这样的事情:
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);