1

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

Calum
  • 1,889
  • 2
  • 18
  • 36
BaSSa
  • 53
  • 1
  • 9
  • You can find your answer here: http://stackoverflow.com/questions/8738369/how-to-add-days-into-the-date-in-android – berserk Jun 17 '14 at 15:09
  • I managed to work it using the link berserk, Thanks, sorry not enough rep to upvote comment – BaSSa Jun 17 '14 at 21:26
  • Glad it helped. And no need to sorry. I am happy that the issue is solved :) – berserk Jun 17 '14 at 21:28

1 Answers1

1

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

  • I dont have a date listener, my xml for the activity has a date picker, a button and a text view, what I am trying to do is when the button is clicked add 282 days to the date in the date picker. – BaSSa Jun 17 '14 at 15:19
  • why do you want to reinventing the wheel again ? you have to look if the day is bigger than 30 or 31 ( sometime 28 or 29 ) and to calculate how many month to add. i would strongly recommend to use a datapicker. dealing with date and time is easy. –  Jun 17 '14 at 15:25