有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在安卓中比较日期而不显示每周的日期

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) {

        }

    }


}

使用这段代码,我显示周一到周日,就像

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 ...

接受但我想要的,如果本周是

2017-7-24 to 2017-7-30

然后,当我们点击下周它应该显示它应该只显示前一周请建议我在这种情况下,我会比较日期


共 (1) 个答案

  1. # 1 楼答案

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

    应用此条件并享受