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

徘徊边缘 提交于 2019-12-13 23:51:43

问题


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

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/42656159/how-do-i-compare-any-date-with-the-current-date-without-taking-the-year-into-ac

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