Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

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

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");
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...