嗨我有一个问题,无论如何我可以从包含日期的textView开始一个日期选择器的日期吗?
这是我的代码:
Event
,包含日期的字符串是txtDate
提前感谢您的帮助! :d
答案 0 :(得分:0)
在实例化日历对象后插入此项。
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dd/MM/yyyy);
Date date = simpleDateFormat.parse(textView.getText().toString());
c.setTime(date);
小心format the SimpleDateFormat以满足您的需求。
答案 1 :(得分:0)
请对您的功能进行以下更改,我已在任何已完成更改的地方提及了注释
public void openDatePicker() {
// Get Current Date
final DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
endDate.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
}
}, mYear, mMonth, mDay);
// get the date from TextView
String startDate = txtDate.getText().toString();
Calendar calendar = Calendar.getInstance();
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
try {
date = simpleDateFormat.parse(startDate); // parse the String according to the desired format
} catch (ParseException e) {
e.printStackTrace();
}
// set the date as minDate
datePickerDialog.getDatePicker().setMinDate(date.getTime());
datePickerDialog.show();
}