Android中使用DateFormat的java.lang.IllegalArgumentException
这是我的代码片段
这里的日期为2013年9月10日09:53:37格式
TextView tvDate = (TextView) convertView.findViewById(R.id.entered_date); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); tvDate.setText(dateFormat.format(salesReportItems.getDate().toString())); TextView tvCardType = (TextView) convertView.findViewById(R.id.card_type); tvCardType.setText(salesReportItems.getCardType().toString());
请帮我解决这个问题。这是我的错误。
亲爱的Piyush,
当我使用你的代码时,这里输出了
创建如下方法
private String formatDate(String dateString) { try { SimpleDateFormat sd = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss" /* 10-Sep-2013 09:53:37*/); Date d = sd.parse(dateString); sd = new SimpleDateFormat("yyyy-MM-dd"); return sd.format(d); } catch (ParseException e) { } return ""; }
然后称之为
tvDate.setText(formatDate(salesReportItems.getDate().toString()));
阅读更多关于如何更改Java中的日期格式?
请参阅此链接并在日期声明中显示并获取任何要求的代码,
Android:我无法弄清楚SimpleDateFormat
尝试使用这样的代码..
如果salesReportItems
是Date
类型对象那么..
String timeStamp = new SimpleDateFormat("yyyy-MM-dd") .format(salesReportItems); tvDate.setText(timeStamp);
我想这一行
tvDate.setText(dateFormat.format(salesReportItems.getDate().toString()));
需要像这样。
tvDate.setText(dateFormat.format(salesReportItems.getDate()));
TextView tvDate = (TextView) convertView.findViewById(R.id.entered_date); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
删除第三行,将日期作为字符串,
String date=salesReportItems.getDate().toString();
使用System.out.println(date);
到目前为止在Logcat中显示;
从观察到的日期forms字符串中得到这样的模式;
sring str="1990-08-27";
然后使用,
tvDate.setText(dateFormat.format(str));
而不是dateFormat.format使用dateFormat.parse(str);
用这个:
public String FormatDate(String dateString) { try { SimpleDateFormat sd = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss""); Date d = sd.parse(dateString); sd = new SimpleDateFormat("yyyy-MMM-dd"); System.out.println(sd.format(d)); return sd.format(d); } catch (Exception e) { } return ""; }
try { SimpleDateFormat sd = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss"); Date d = sd.parse(salesReportItems.getDate().toString()); sd = new SimpleDateFormat("yyyy-MM-dd"); TextView tvDate = (TextView) convertView.findViewById(R.id.entered_date); tvDate.setText(sd.format(d)); } catch (Exception e) { e.printStackTrace(); }
问题按上述代码排序。 谢谢亲爱的Piyush Gupta.your指南支持我解决这个问题:-)