0

I am wanting to create a gestation period calculator for my app. I want to grab the date that the user had picked in the date time picker and advance that date by 9 months and 10 days and print it out in a textView. I am able to get the date from the date picker and print the date into the textView but what I need to do now is to advance the date by the 9 months and 10 days. Any ideas? and here is my current code for getting the date from the date picker and printing it to a text view.

public class GestationPeriod extends Activity {
DatePicker date;
TextView gestationperiodView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gestationperiod);
    setupVariables();
}

public void calculate(View v){      
    int gestationPeriodMonth = 9;
    int gestationPeriodDay = 10;
    int year = date.getYear();
    int month = date.getMonth();
    int day = date.getDayOfMonth(); 
    gestationperiodView.setText("" + day + " / " + month + " / " + year);

}
private void setupVariables(){      
    date = (DatePicker) findViewById(R.id.datePicker1);
    gestationperiodView = (TextView) findViewById(R.id.editText1);  
}      

Your help would be much appreciated.

4

1 に答える 1

1

java.util.Calendar.add(int field、int amount)を使用します。日、月、年を取得した後、その前に、次gestationperiodView.setTextを挿入します。

Calendar c = new Calendar();
c.set(year, month-1, day);
c.add(Calendar.DAY_OF_MONTH, gestationPeriodDay);
c.add(Calendar.MONTH, gestationPeriodMonth);
day = c.get(Calendar.DAY_OF_MONTH);
month = c.get(Calendar.MONTH) + 1;
year = c.get(Calendar.YEAR);
于 2012-07-02T07:56:15.153 に答える