0

I have this code, I want to add 40 weeks to the date I get from the date Picker and get the new date after the 40 weeks (280 days) has been added to the date from the date picker.

Code:

public class MainActivity extends AppCompatActivity {

    DatePickerDialog picker;
    EditText eText;
    Button btnGet;
    TextView tvw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvw=(TextView)findViewById(R.id.textView1);
        eText=(EditText) findViewById(R.id.editText1);
        eText.setInputType(InputType.TYPE_NULL);
        eText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Calendar cldr = Calendar.getInstance();
                int day = cldr.get(Calendar.DAY_OF_MONTH);
                int month = cldr.get(Calendar.MONTH);
                int year = cldr.get(Calendar.YEAR);
                // date picker dialog
                picker = new DatePickerDialog(MainActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                eText.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
                            }
                        }, year, month, day);
                picker.show();
            }
        });
        btnGet=(Button)findViewById(R.id.button1);
        btnGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvw.setText("Selected Date: "+ eText.getText());
            }
        });
    }
}
Nikunj Paradva
  • 15,332
  • 5
  • 54
  • 65
  • 1
    You might want to look at this. In your `onDateSet` handler, construct a `Calendar` object from the year, month and day and add number of days as desired. Look at this answer. https://stackoverflow.com/questions/9670648/modify-the-week-in-a-calendar – Deepak Khillare May 14 '19 at 09:13

4 Answers4

3

Joda time is a very convenient library for handling such cases. Add this to your project:

dependencies {    
    compile 'joda-time:joda-time:2.10.2'
}

And then you can manipulate the dates like this:

DateTime dt = DateTime.now();
DateTime laterDate = dt.withYear(2020)
    .withMonthOfYear(3)
    .withDayOfMonth(14)
    .plusWeeks(40);

Remember that in JodaTime date objects are immutable (which is a very good idea), so each manipulation produces a new object.

mate00
  • 2,727
  • 5
  • 26
  • 34
1

First, convert the current format to milliseconds and then add specific days milliseconds and then again get it in the desired format. Like this way:

new DatePickerDialog(MainActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

        Calendar calendar = Calendar.getInstance();
        calendar.set(year,monthOfYear + 1,dayOfMonth);

        long timeInMilliseconds = 
        calendar.getTimeInMillis()+TimeUnit.DAYS.toMillis(280);

        calendar.setTimeInMillis(timeInMilliseconds);

        int mYear = calendar.get(Calendar.YEAR);
        int mMonth = calendar.get(Calendar.MONTH);
        int mDay = calendar.get(Calendar.DAY_OF_MONTH);
                                eText.setText(mDay + "/" + mMonth + "/" + mYear);
                            }
                        }, year, month, day);
                picker.show();
            }
        });
Sultan Mahmud
  • 1,245
  • 8
  • 18
1

Use add(Calendar.DAY_OF_MONTH, int) function in this way:

cldr.add(Calendar.DAY_OF_MONTH, 280);
kAvEh
  • 534
  • 4
  • 15
0

Logically speaking, you don't have to add 40 weeks per say, a week has specific number of days, thus you can just add 40*7 = 280 days in your current day picked up from date picker.

[current date] + TimeUnit.DAYS.toMillis(280)
Milan Desai
  • 1,228
  • 8
  • 23