I am quite new to Android development, but the person has written the code is away and i have taken over this job.
There is one thing I would like to find out quickly…(–
The app is picking up the user input of a date (using a date picker) and i need add a validation to check the if the date is valid. The valid dates are 30 days from today.
After searching on the internet for long time, i’ve found a code i might can use:
Date today = new Date();
Date predefined = new SimpleDateFormat("yyyy-MM-dd").parse(today);
if(today.before(predefined)) {
...
}
But I am not sure how to add 30 days?
If you could tell me, that would be much appreciated.
Thanks in advance.
Edit Here is the Source code I’ve tried.
Calendar today = Calendar.getInstance();
today.add(Calendar.DAY_OF_MONTH,30);
if(calStartDate.compareTo(today)<0) {
Toast.makeText(GetClient.this,"It's before valid date!",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(GetClient.this,"It's a valid date!",Toast.LENGTH_SHORT).show();
}
You want the Calendar class. You can create one and set it to current time/date, and create another and set roll it forward 30 days. Then call compareTo() on one passing in the other.