BaSSa
BaSSa

Reputation: 53

Adding Days To Date Picker date

I need help adding 282 days to this code

public void onClick_add282(View v) {


TextView tv_output; 
tv_output = (TextView) findViewById(R.id.tv_output);

dPicker = (DatePicker)findViewById(R.id.dp_cal);

Integer dYear = dPicker.getYear();
Integer dMonth = dPicker.getMonth()+1;
Integer dDate = dPicker.getDayOfMonth();

StringBuilder ad=new StringBuilder();
ad.append(dDate.toString()).append("-").append(dMonth.toString()).append("-").append(dYear.toString()).append("");
String dStr=ad.toString();
tv_output.setText(dStr);



}

I tried to just add 282 to this line like this

    Integer dDate = dPicker.getDayOfMonth()+282;

and it returned 300-6-2014(today being the 18-6-2014 where I live)

EDIT: I'm not trying to add to the current date but to a date selected by the user

Upvotes: 1

Views: 1256

Answers (1)

user2035951
user2035951

Reputation:

hmm ? maybe you mixed up something. did you setup a date listener ?

here a code snipped i do it:

some global variables:

public int gl_year = -1, gl_month = -1, gl_day = -1;

this is the definition of a button to open the date picker

    datePickerButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (debug)
                Log.d(TAG, "datePickerButton.setOnClickListener() ");
            new DatePickerDialog(MainActivity.this, dateListener, gl_year,
                    gl_month, gl_day).show();
        }
    });

and thats the listener

private DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        int displMonth = monthOfYear + 1;
        if (debug)
            Log.d(TAG, "onDateSet() " + year + "/" + displMonth + "/"
                    + dayOfMonth);
        Calendar selectedDate = null;
        selectedDate = Calendar.getInstance();
        gl_year = year;
        gl_month = monthOfYear;
        gl_day = dayOfMonth;
        selectedDate.set(gl_year, gl_month, gl_day);
        calendarView.setDate(selectedDate.getTimeInMillis());
    }
};

if you add to the integer variable gl_day the value of 282 BEFORE opening date picker dialog it should do the work. if not change the calender object to time of milliseconds and add the value of 24364800000

Upvotes: 1

Related Questions