-1

I want to Get the Current Date from user after Getting the Current Date The Text view Want to Display Automatically the Date with 100 Days

ie if i select 15-12-2015 the Text view Want to Display 24-03-2016

here is My Code:

public class MainActivity extends ActionBarActivity implements View.OnClickListener {
    private Button ib;
    private Calendar cal;
    private int day;
    private int month;
    private int year;
    private EditText et;
    private TextView display;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ib = (Button) findViewById(R.id.button);
        cal = Calendar.getInstance();
        day = cal.get(Calendar.DAY_OF_MONTH);
        month = cal.get(Calendar.MONTH);
        year = cal.get(Calendar.YEAR);
        et = (EditText) findViewById(R.id.editText);
        display = (TextView) findViewById(R.id.textView3);
        ib.setOnClickListener((View.OnClickListener) this);

    }
@Override
    public void onClick(View v) {
        showDialog(0);
    }
    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {
        return new DatePickerDialog(this, datePickerListener, year, month, day);
    }

    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int selectedYear,
                              int selectedMonth, int selectedDay) {
            display.setText(selectedDay + 100) + " / " + (selectedMonth ) + " / "
                    + selectedYear;
        }
    };
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Spartons
  • 85
  • 10

3 Answers3

1

Use Calender Class for adding number into date. Be ensure date is in well formated.

    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, int selectedYear,
                                  int selectedMonth, int selectedDay) {
                  String dtStartDate = selectedYear+"-"+selectedMonth+"-"+selectedDay+"00:00";
                  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                  Calendar c = Calendar.getInstance();
                  c.setTime(dtStartDate);
                  c.add(Calendar.DATE, 100);  // number of days to add
                  String dt = sdf.format(c.getTime());  // dt is now the new date
                  display.setText(dt);
            }
        };
Amy
  • 4,034
  • 1
  • 20
  • 34
1

Try something like this

Date dtStartDate=//User inputs start date. 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

Calendar c = Calendar.getInstance();
c.setTime(dtStartDate);
c.add(Calendar.DATE, 100);  // number of days to add
String datePlus100 = sdf.format(c.getTime());  // datePlus100 shows date inputted + 100 days!
Zain
  • 2,336
  • 1
  • 16
  • 25
  • It Display Date but it Display Same Date When i selected Another Date..ie if i select Today Date it Display 2016-03-24 when i select 3 days after Today it Display the Same Date 2016-03-24..It means My Listner is Not called Everytime how To Solve this?? @Zain – Spartons Dec 15 '15 at 11:24
0

This code works. I can't get how listener can not handle action in your solution

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;

@Override
public void onClick(View v) {
    showDialog(0);
}

@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
      if (id == 0) {
        return new DatePickerDialog(MainActivity.this, datePickerListener, year, month, day);
      }
      return super.onCreateDialog(id);
    }

OnDateSetListener myCallBack = new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
    int dayOfMonth) {
  display.setText("set date from calendar as answered upper");
  }
};
Stanislav Bondar
  • 6,056
  • 2
  • 34
  • 46