我很难从datepicker获取日期并添加修复日期并显示在文本字段中。
我有两个文本框和一个按钮。我在按钮上实现了datepicker对话框,并在一个文本框中显示该日期,但我想在datepicker日期中添加270天并在textbox2上显示新日期。我想在newdate textfield中添加270天datepicker_date和diplay。
这是我的程序
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(" "));
}
答案 0 :(得分: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();
....
答案 1 :(得分: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()));