Reputation: 3
I've made this function that compares 3 dates but its not working and I cant figure out why. data = 2013-02-10
and data2= 2013-02-14
, datateste
is the date of the system. it should appear an error alert but it doesn't
void restricoes() {
Button btnCreateProduct = (Button) findViewById(R.id.button2);
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd");
java.util.Date strDate1 = sdf.parse(data);
java.util.Date strDate2 = sdf.parse(data2);
java.util.Date strDate = sdf.parse(datateste);
if (strDate.before(strDate1) && strDate.after(strDate2)) {
Toast.makeText(getApplicationContext(), "chupa boi",
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), data,
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), data2,
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), datateste,
Toast.LENGTH_LONG).show();
inputdata.setError("yipikaei");
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(checkEditText(inputdtestab) || checkEditText(quantidade))
{
inputdtestab.setError("yipikaei");
quantidade.setError("mudfukkerrr");
btnCreateProduct.setVisibility(View.INVISIBLE);
}
else{
// ==========================================
// make button visible.
// use the method "setVisibility", not "setViewVisibility"
// and "VISIBLE" in "View.VISIBLE" is in full cap:
btnCreateProduct.setVisibility(View.VISIBLE);
}
}
so now instead of this i putted the code directly in the oncreate method and it works
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date1 = sdf.parse(datateste);
java.util.Date date2 = sdf.parse(data);
System.out.println(sdf.format(date1));
System.out.println(sdf.format(date2));
if(date1.compareTo(date2)>0){
inputdata.setError("asa");
}else{
System.out.println("How to get here?");
}
}catch(ParseException ex){
ex.printStackTrace();
}
but if change the data to not respect the if condition the error alert doesnt disappear
Upvotes: 0
Views: 53
Reputation: 370
As you said, data = "2013-02-10"
, but you seem to be parsing with the pattern "yyyy/MM/dd"
.
Try with "yyyy-MM-dd"
or change your input data.
Upvotes: 0
Reputation: 97130
Your date format uses this pattern "yyyy/mm/dd"
. However, mm
stands for minutes. If you need months, you need to uppercase it to "yyyy/MM/dd"
. See the documentation here.
Furthermore, you're checking whether 2014/06/11 is both before 2013/2/10 and after 2013/2/14. That will obviously never be the case.
Upvotes: 1