如何增加从日期选择器中选择的日期?

时间:2017-04-14 12:47:51

标签: android

以下是我的代码。我想在这里实现的是我从日期选择器中选择一个日期,我将其设置为curretToday。但是,当我转到下一个约会时,它会延长今天的日期,而不是递增所选日期。我不知道我哪里出错了。请帮帮我。

    getDate(icount);

    iv_refresh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            icount = 0;
            getDate(icount);
        }
    });

    rightarrow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


                getDate(++icount);

        }
    });

    leftarraow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getDate(--icount);
        }
    });



    appTitleTextView = (TextView) findViewById(R.id.tool_bar_app_title_textview);

    toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close);
}
private void getDate(int i) {

    if(new DatePickerFragment(tv_date).datePickerDate == null) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, i);
        currentToday = dateFormat.format(calendar.getTime());
        tv_date.setText(currentToday);


    } else {

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR,i);
        currentToday =new DatePickerFragment(tv_date).datePickerDate;
        tv_date.setText(currentToday);


    }

2 个答案:

答案 0 :(得分:1)

这将帮助您增加日期

public Date getIncrementedDate(Date selectedDate) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.setTime(sdf.parse(selectedDate));
    c.add(Calendar.DATE, 1);  // number of days to add
    selectedDate = sdf.format(c.getTime());  // selectedDate is now the new date
return selectedDate;
}

答案 1 :(得分:0)

您可以使用下一个代码:

public Date incrementDay(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.add(Calendar.DAY_OF_YEAR, 1); 
    return new Date(c.getTimeInMillis());
}