Comparing dates?

爷,独闯天下 提交于 2019-12-02 13:36:46

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.

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.

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;
            }

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();
              }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!