-3

I’m trying to compare two dates in Android but im getting this

notification

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

try n catch

I can’t compare the date because it says I didn't declared strDate.

can't compare

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jun 15 '18 at 09:26
  • 1
    It’s hard to tell from your detached pieces of code what you are missing. We can help you better if you [create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Ole V.V. Jun 15 '18 at 09:35
  • You can find good inspiration in [this answer](https://stackoverflow.com/a/38334978/5772882) and generally use your search engine to find much more. Similar questions have been asked and answered tons of times. – Ole V.V. Jun 15 '18 at 09:40
  • Possible duplicate of [Cannot find Symbol - Variable, despite the variable being declared](https://stackoverflow.com/questions/34688263/cannot-find-symbol-variable-despite-the-variable-being-declared) – Ole V.V. Jun 15 '18 at 14:06
  • On Stack Overflow, you should post source code and error messages as text, not screenshots. Stack Overflow has features to format such text, based on Markdown syntax. – Basil Bourque Jun 16 '18 at 15:18

4 Answers4

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.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

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;
            }
Learning Always
  • 1,563
  • 4
  • 29
  • 49
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();
              }
        }
Omkar
  • 3,040
  • 1
  • 22
  • 42