简体   繁体   中英

how to compare date and not display feauter week date in android

public class TestActvity extends AppCompatActivity implements View.OnClickListener {

    Button next;
    Button previous;
    TextView textView;
    int interval = 2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        next = (Button) findViewById(R.id.next);
        previous = (Button) findViewById(R.id.previous);
        next.setOnClickListener(this);
        previous.setOnClickListener(this);
        textView = (TextView) findViewById(R.id.textView);
        getDate(interval);
    }

    @Override
    public void onClick(View view) {

        if (view.getId() == R.id.next)

        {
            interval = interval + 7;
            getDate(interval);

        } else if (view.getId() == R.id.previous) {
            interval = interval - 7;
            getDate(interval);

        }
    }

    public void getDate(int interval) {

        try {
            Calendar c = GregorianCalendar.getInstance();
            c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            c.add(Calendar.DAY_OF_WEEK, interval - 2);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            String startDate = "", endDate = "";
            startDate = df.format(c.getTime());
            c.add(Calendar.DATE, 6);
            endDate = df.format(c.getTime());
            textView.setText(startDate + "  to  " + endDate);
            Date date = new Date();
            String modifiedDate = new SimpleDateFormat("yyyy-MM-dd").format(date);

            if ( endDate.compareTo(modifiedDate) > 0) {
                Log.d("data", "" + "");
            }
            Log.d("data", "" + "");
        } catch (Exception e) {

        }

    }


}

Using this code, i am displaying Monday to Sunday like

2017-7-3 to 2017-7-9 ,2017-7-10 to 2017-7-16 ,2017-7-17 to 2017-7-23 ,2017-7-24 to 2017-7-30 ,2017-7-31 to 2017-8-5 ...

Receptively but what I want, if current week is

2017-7-24 to 2017-7-30

then when we click on next week it should display it should display only previous week please suggest me in this scenario I will compare date.

    if (c.getTime().after(new Date())){
        next.setVisibility(View.GONE);
    }else {
        next.setVisibility(View.VISIBLE);
    }

apply this Condition and enjoy !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM