0

Hey guys i am working on an application that can be used to predict the due date of a babies birth but i have failed to figure out how to increment the date from the date time picker dialog below is my code

public class Dday extends Activity implements OnClickListener{

private EditText fromDateEtxt;

private DatePickerDialog fromDatePickerDialog;

private SimpleDateFormat dateFormatter;

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

    dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);

    findViewsById();

    setDateTimeField();
}

private void findViewsById() {
    fromDateEtxt = (EditText) findViewById(R.id.etxt_fromdate);    
    fromDateEtxt.setInputType(InputType.TYPE_NULL);
    fromDateEtxt.requestFocus();
}

private void setDateTimeField() {
    fromDateEtxt.setOnClickListener(this);

    Calendar newCalendar = Calendar.getInstance();
    fromDatePickerDialog = new DatePickerDialog(this, new OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar newDate = Calendar.getInstance();
            newDate.set(year, monthOfYear, dayOfMonth);
            fromDateEtxt.setText(dateFormatter.format(newDate.getTime()));
        }

    },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.dday, menu);
    return true;
}

public void showDate (View view)
{
    // Adding the days to the date 

    // Button action

    Intent dts = new Intent(this,Details.class);

    startActivity(dts);
}

@Override
public void onClick(View view) {
    // TODO Auto-generated method stub

    if(view == fromDateEtxt) {
        fromDatePickerDialog.show();
    }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92

3 Answers3

0

You can increment a Calendar object by 39 weeks with something like this:

Calendar cal = Calendar.getInstance();
cal.add(Calendar.WEEK_OF_YEAR, 39);
M3SSYM4RV1N
  • 135
  • 10
0
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
    Calendar c = Calendar.getInstance();
    c.set(selectedYear, selectedMonth, selectedDay);
    c.add(Calendar.DATE, 39);
}

for more see this thread How do I set the date for a date picker?

Community
  • 1
  • 1
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46
  • Hey just a quick one the code above if i just added it to my code would it work fine ? direct into this public void showDate (View view) { // Adding the days to the date // Button action Intent dts = new Intent(this,Details.class); startActivity(dts); } can than work ? – Joe Kasedde Sep 16 '15 at 08:09
  • yes, you can update the date in you showdate method. But for his you have to keep the reference of user selected date in calendar. – Deepak Goyal Sep 16 '15 at 16:21
0

You can use the Calendar Object and create a date from Today and add 39 weeks using the Calendar class by setting Date objects based on the Calendar and modified Calendar like so:

    // Formatter for readable comparison
    DateFormat sdf = new SimpleDateFormat("MM dd yyyy", Locale.getDefault());

    // Today
    Calendar now = Calendar.getInstance();
    Date dateToday = new Date();
    dateToday.setTime(now.getTimeInMillis());

    // 39 weeks from today
    Date futureDate = new Date();
    // Calendar supports roll over with year so no need to worry about modifying year separate
    now.add(Calendar.WEEK_OF_YEAR, 39); 
    futureDate.setTime(now.getTimeInMillis());

    // Print out for comparison
    System.out.println(sdf.format(dateToday));
    System.out.println(sdf.format(futureDate));

Which prints out:

I/System.out﹕ 09 16 2015 <-- Today
I/System.out﹕ 06 15 2016 <-- 39 weeks later
kandroidj
  • 13,784
  • 5
  • 64
  • 76
  • great work! Thank you very much and would you mind helping me check if my sample app meets basic requirements and may be patch it where you find loop holes ? – Joe Kasedde Sep 17 '15 at 00:08