问题
I’m trying to compare two dates in Android but im getting this
when I write this
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
String valid_until = "26052018";
final Date strDate = sdf.parse(valid_until);
and if I put the try and catch like this
I can’t compare the date because it says I didn't declared strDate
.
回答1:
SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date → text), parsing (text → date), and normalization.
Good Approach
- You should use PROPER Date format.
DEMO
Date strDate=null; // Declare as global;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String valid_until = "26/05/2018";
strDate = sdf.parse(valid_until);
if (new Date().after(strDate)) {
}
You can visit Best way to compare dates in Android.
回答2:
tl;dr
Your problem is not really about comparing dates. Your problem is trying to reference a local variable from outside its scope.
Scope
In your onClick
method where you try to use strDate
:
if( new Date().after( strDate ) )
…there is no such variable strDate
within scope. Methods do not see local variable defined within other methods(*). That is why they are called “local”, they do not exist outside the method that defined them.
Scope can be even narrower. Local variables declared inside curly braces, such as on an if
statement, do not exist outside those curly braces.
To access strDate
from another method, you must make it available it available. One way is to pass the variable as an argument when calling the method. Another way is to declare the variable as a member of the class, if both places where you populate and use the variable live within the same class.
java.time
As others noted, you should avoid the terribly troublesome old date-time classes such as java.util.Date
. These were supplanted years ago by the industry-leading java.time classes.
LocalDate.parse(
"26052018" ,
DateTimeFormatter.ofPattern( "ddMMuuuu" )
)
Search Stack Overflow to learn about parsing strings into LocalDate
objects.
And avoid using custom date formats as seen in your Question. Whenever possible, stick with standard ISO 8601 formats. For a date-only value, that would be YYYY-MM-DD. Again, search Stack Overflow to learn.
LocalDate.parse( "2018-05-26" )
(*) There are some roundabout ways for methods to see local variables defined in other methods. These include reflection/introspection and inner classes. But they are special cases.
回答3:
use this to compare two date
First convert String to date object
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss", Locale.US);
Date d1, d2;
try {
d1 = inputFormat.parse(yourStringDate);
d2 = inputFormat.parse(yourStringDate);
return d2.compareTo(d1);
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
回答4:
try this
@Override
public void onClick(View v) {
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
String valid_until = "26052018";
Date strDate = null;
try {
strDate = sdf.parse(valid_until);
if(new Date().after(strDate)){
startActivity(new Intent(MainActivity.this,Error.class));
}
} catch (ParseException e) {
e.printStackTrace();
}
}
来源:https://stackoverflow.com/questions/50869853/comparing-dates