1

我无法从 datepicker 获取日期并添加修复天数并显示在文本字段中。

我有一个两个文本框和一个按钮。我在按钮上实现 datepicker 对话框并在一个文本框中显示该日期,但我想在 datepicker 日期中添加 270 天并在 textbox2 上显示新日期。我想在 datepicker_date 中添加 270 天并在 newdate 文本字段中显示。

这是我的程序

public void setCurrentDateOnView() {

    datepicker_date = (TextView) findViewById(R.id.datepicker);
            newdate = (TextView) findViewById(R.id.datepicker1);

    //dpResult = (DatePicker) findViewById(R.id.dpResult);

    final Calendar c = Calendar.getInstance();
    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);

    // set current date into textview
    datepicker_date.setText(new StringBuilder()
            // Month is 0 based, just add 1
            .append(month + 1).append("-").append(day).append("-")
            .append(year).append(" "));
    // set current date into datepicker
}

public void addListenerOnButton() {

    date = (Button) findViewById(R.id.date);

    date.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            showDialog(DATE_DIALOG_ID);

        }
    });
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        // set date picker as current date
        return new DatePickerDialog(this, datePickerListener, year, month,
                day);


    }
    return null;
}

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

    // when dialog box is closed, below method will be called.


    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
        year = selectedYear;
        month = selectedMonth;
        day = selectedDay;

        // set selected date into textview
        datepicker_date.setText(new StringBuilder().append(month + 1)
                .append("-").append(day).append("-").append(year)
                .append(" "));


        // set selected date into datepicker also
        //dpResult.init(year, month, day, null);

    }
};

 public void setCurrentDate1OnView() {

        newdate = (TextView) findViewById(R.id.datepicker1);
        //dpResult = (DatePicker) findViewById(R.id.dpResult);

        final Calendar c = Calendar.getInstance();
        year1 = c.get(Calendar.YEAR);
        month1 = c.get(Calendar.MONTH);
        day1 = c.get(Calendar.DAY_OF_MONTH);

        // set current date into textview

        newdate.setText(new StringBuilder()
        // Month is 0 based, just add 1
        .append(month1 + 1).append("-").append(day1).append("-")
        .append(year1).append(" "));
}
4

2 回答 2

0

为您的目的使用joda-time包。

import org.joda.time.LocalDateTime;
....

Calendar cal = Calendar.getInstance();
long timeInMillA = cal.getTimeInMillis();
LocalDateTime startA = new LocalDateTime(timeInMillA);

//add 270 days:
startA.plusDays(270);
.... 

Period p = new Period(startA);
  p.getMonths();
  p.getYears();
  p.getWeeks();
  p.getHours();
  p.getMinutes();
  ....
于 2012-12-04T11:38:34.867 回答
0

您可以通过这种方式将 270 天添加到所选日期:

public void onDateSet(DatePicker view, int selectedYear,
                      int selectedMonth, int selectedDay) {
    Calendar c = Calendar.getInstance();
    c.set(selectedYear, selectedMonth, selectedDay);
    c.add(Calendar.DAY_OF_MONTH, 270);
}

此外,您可以以更简单的方式格式化显示日期,而不是通过连接字符串来搞乱:

import java.text.DateFormat;

textView.setText(DateFormat.getDateInstance().format(calendar.getTime()));

如果您希望使用特定格式,则:

SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy", Locale.US);
textView.setText(sdf.format(calendar.getTime()));
于 2012-12-04T11:49:38.703 回答