使用getAttributeValue时如何正确格式化日期?

时间:2014-11-23 22:30:11

标签: android simpledateformat xmlpullparser

我有一个xml文件,其中有一个日期属性,并使用XmlPullParser在列表中显示包含日期" yyy / MM / dd HH:mm"的数据,但我想条件只选择过去30天的数据,我比较了xml文件中的日期。问题是当我想将文件日期格式化为" yyy / MM / dd"时。我做下一个代码,但它不起作用。我该怎么做才能正确格式化日期?

SimpleDateFormat sdfDateBefore = new SimpleDateFormat("yyyy/MM/dd", Locale.US);
String date = xpp.getAttributeValue(null, "fecha");
String DateFile = sdfDateBefore.format(date);

1 个答案:

答案 0 :(得分:0)

我可以解决如下问题:首先我必须转换Date对象中的原始字符串值,然后转换为String对象。

SimpleDateFormat sdfDateBefore = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.US);
sdfDateBefore.setTimeZone(TimeZone.getTimeZone("UTC"));

SimpleDateFormat dateTodayFile = new SimpleDateFormat("yyyy/MM/dd", Locale.US);
String fechaFile = xpp.getAttributeValue(null, "fecha");
Date date = null;
try {
  date = sdfDateBefore.parse(fechaFile);
} catch (ParseException e) {
  e.printStackTrace();
}
String DateFile = dateTodayFile.format(date);