我想为我的应用程序创建一个孕育期计算器。我想获取用户在日期时间选择器中选择的日期,并将该日期提前9个月零10天,然后在textView中打印出来。我可以从日期选择器中获取日期,并将日期打印到textView中,但我现在需要做的是将日期提前9个月零10天。有什么想法吗?下面是我当前的代码,用于从日期选择器获取日期并将其打印到文本视图。
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);
} 您的帮助我们将不胜感激。
发布于 2012-07-02 15:56:15
使用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);https://stackoverflow.com/questions/11289511
复制相似问题