-2

I want to compare the date with the current date, without taking the year into account, and I want to change the text for each day

Here is my code

TextView textView = (TextView)findViewById(R.id.textView);

        String valid_until = "7/3";
        String valid_until1 = "8/3";
        String valid_until2 = "9/3";
        String valid_until3 = "10/3";

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM");
        Date strDate = null;
        Date strDate1 = null;
        Date strDate2 = null;
        Date strDate3 = null;


        try {
            strDate = sdf.parse(valid_until);
            strDate1 = sdf.parse(valid_until1);
            strDate2 = sdf.parse(valid_until2);
            strDate3 = sdf.parse(valid_until3);


        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (new Date() == (strDate)) {
            textView.setText("7 m");
        }
        if (new Date() == (strDate1)) {
            textView.setText("8 m");
        }
        if (new Date() == (strDate2)) {
            textView.setText("9 m");
        }
        if (new Date() == (strDate3)) {
            textView.setText("10 m");
        }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Duplicate of: [how to compare current day & month with given day & month in java ](http://stackoverflow.com/a/39231958/642706) – Basil Bourque Mar 07 '17 at 23:16

2 Answers2

0

How do I compare any date with the current date, without taking the year into account

One option, you could use a Calendar object. Set the date on each, and then simply compare the "DAY_OF_YEAR" value.

https://developer.android.com/reference/java/util/Calendar.html#DAY_OF_YEAR

whizzle
  • 2,053
  • 17
  • 21
-1

tl;dr

MonthDay.now( ZoneId.of( "America/Montreal" ) )
        .equals( MonthDay.of( Month.JULY , 3 ) )

Details

See my Answer on a duplicate question for details. Briefly recapped here…

Avoid the troublesome old legacy classes Date & SimpleDateFormat like the plague. They have been supplanted by the java.time classes. Much of java.time has been back-ported to Android in the ThreeTenABP project.

Do not capture the current date repeatedly. If your code were to run over midnight, you would get inconsistent results as the date changed.

The java.time.MonthDay class solves your problem, along with LocalDate and ZoneId.

ZoneId z = ZoneId.of( "America/Montreal" );  // Zone in which you want the current date.
LocalDate today = LocalDate.now( z ); // Current date for your zone.
MonthDay mdCurrent = MonthDay.of( today );
MonthDay mdJulyThird = MonthDay.of( Month.JULY , 3 );
if( mdCurrent.equals( mdJulyThird) ) { … }

To address your specific problem, I would make a collection of Month enum objects and loop. Search Stack Overflow for much more info on EnumSet.

EnumSet<Month> months = EnumSet.range( Month.JULY , Month.OCTOBER );
for ( Month month : months ) {
    MonthDay md = MonthDay.of( month , 3 ) ; 
    if( md.equals( mdCurrent ) { 
        … 
        break; // Break out of the 'for' loop. No need to continue looping.
    }
}

For generating strings, call MonthDay::format and pass a DateTimeFormatter object. Search Stack Overflow for much more info on DateTimeFormatter on many hundreds of questions & answers.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Dear Down-Voter, please leave a criticism along with your vote. – Basil Bourque Mar 08 '17 at 02:27
  • Can you show me an example of comparing several days with the current date ... so that I could print out the text for each day? I need the program to show the time of sunrise and sunset for every day of the year ... – Мухаммаднакшубанди Омаров Mar 08 '17 at 11:29
  • @МухаммаднакшубандиОмаров I already did show comparing multiple month-day objects. Look at the EnumSet code. And "every day of the year" is a different question, one that has already been asked and answered in other Questions on Stack Overflow. See the [`Year`](https://docs.oracle.com/javase/8/docs/api/java/time/Year.html) class and its `atDay` & `length` methods. – Basil Bourque Mar 08 '17 at 13:44