I am getting a date from JSON file, and it is in string format.I have two string values of date named startdate and enddate that is coming from intent to currentActivity. Now I want to check that, if date value coming from json file is after the startdate or before the enddate. How can I do this? And yes, the format I json file having is “yyyy-mm-dd”.
JSON Data Look like:
{“posts”:[{“start_date”:”2013-02-15″,”end_date”:”2013-02-21″}]}
Here is the code I have tried but I am getting output as Mon Jan 07 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();
}
Here is the code of comparing date:
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();
}
}
I would prefer using a more robust approach that involves converting the date strings to actual
Date(orCalendar) objects:From here on, you can use e.g.
start.before(end)orend.after(start)to check whether a date comes before or after another date. If you need more fine-grained control, you can always get the date in milliseconds and have your logic work on that.So you’ve updated your code, but you’re leaving it up to us to figure out what’s going on and, more importantly, what you’re expecting to happen? It looks like you want to check how the current date relates to those retrieved from json, although I’m a little lost at what the fourth date field, named
date, is for.Just some remarks about the current snippet:
Personally, I find
before()andafter()much more readable and descriptive, but I suppose there’s nothing wrong with usingcompareTo(). However, important to realize is that0will only be returned iff the underlying millisecond representations of two dates are equal. With that being said, I’m not sure how much sense it would make to do the first check in the following condition:You’ll have to be really lucky to get the milliseconds of
endexactly identical tocurrDate. Unless you do some manipulation somewhere to normalize all the dates to e.g. midnight, it’s unlikelyend.compareTo(currDate) == 0will ever be true.Regarding this normalization: have a look at the previously mentioned
Calendarclass. It’ll allow you to easily retrieve the values for the separate fields of a datestamp/timestamp. For example, if you only want to compare the day, month and year, you can get those specific fields from aCalendarinstance with a simple get call. Even more convenient is that you can also set every field independently – that’s great for normalizing all dates to e.g. midnight or midday, after which you can still use thebefore()andafter()methods.I’m convinced that should give you enough pointers to correctly code up an implementation that fits your needs. Without exactly knowing what you’re trying to achieve, I’m afraid I can’t help you any further.