我从JSON文件获取日期,并且它是字符串格式。我有两个来自intent currentActivity的名为startdate和enddate的日期字符串值。现在我想检查一下,如果来自json文件的日期值在startdate之后或enddate之前。我怎样才能做到这一点?是的,我的json文件的格式是“yyyy-mm-dd”。如何检查日期(从json文件获取)是在android之前还是之后?
JSON数据如下所示:
{ “职位”:[{ “起始日期”: “2013年2月15日”, “END_DATE”: “2013年2月21日”}]}
这里是代码我都试过,但我得到输出星期一2007年1月00:00:00 GMT + 5:30 2013:
Intent intent = getIntent();
String startDate = intent.getStringExtra("startDate"); //I have check this. It is proper.
String endDate = intent.getStringExtra("endDate"); //I have check this. It is proper.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date start = null,end = null;
try {
start = sdf.parse(startDate);
end = sdf.parse(endDate);
} catch (ParseException e1) {
e1.printStackTrace();
}
这里是比较日期代码:
Date date = null;
try {
date = sdf.parse(c.getString(TAG_DATE)); //I am getting the date from a json file here.
} catch (ParseException e) {
e.printStackTrace();
}
Date currDate = new Date();
if (end.compareTo(currDate) < 0 || start.compareTo(currDate) < 0) {
Toast.makeText(getApplicationContext(),"Please select valid dates...",Toast.LENGTH_SHORT).show();
} else if (end.compareTo(currDate) == 0 && start.compareTo(currDate) >= 0){
if (date.after(start)) {
Toast.makeText(getApplicationContext(), "After...",Toast.LENGTH_SHORT).show();
}
} else if (end.compareTo(currDate) > 0 && start.compareTo(currDate) >= 0) {
if (date.after(start) && date.before(end)) {
Toast.makeText(getApplicationContext(),"Before...",Toast.LENGTH_SHORT).show();
}
}
2013-01-03
Dhwani
+0
你可以在JSON文件中发布日期格式的样本吗? –
+0
雅我已经把它。 –
+0
什么行给出你描述的输出? –