我建立了一个日期选择器,让用户选择日期并将其返回(“dd-MM-yyy”);.但是,日期选择器从1990年开始,我正在尝试将默认开始日期更改为当天。
private void SetDatePicker() {
DatePickerDialog.OnDateSetListener listner = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
currentDate = new SimpleDateFormat("dd/MM/yyyy");
currentday = new SimpleDateFormat("dd");
currentMonth = new SimpleDateFormat("mm");
currentYear = new SimpleDateFormat("yy");
todayDate = new Date();
String thisDate = currentDate.format(todayDate);
Log.w ("Date", thisDate);
DateStore.set (Calendar.DAY_OF_MONTH, Integer.parseInt (currentday.format(todayDate)));
DateStore.set (Calendar.MONTH, Integer.parseInt (currentMonth.format(todayDate)));
DateStore.set (Calendar.YEAR, Integer.parseInt (currentYear.format(todayDate)));
DayofMonth = DateStore.get (Calendar.DAY_OF_MONTH);
MonthOfMonth = DateStore.get (Calendar.MONTH) + 1;
mYear = DateStore.get (Calendar.YEAR);
String DateString = String.valueOf (dayOfMonth) + "-" + String.valueOf (MonthOfMonth) + "-" + String.valueOf (year);
Log.w ("Date Test" , DateString);
}
};
DatePickerDialog DatePickerDioalg = new DatePickerDialog (getActivity() ,0,listner ,Integer.parseInt (currentday.format(todayDate)),
Integer.parseInt (currentMonth.format(todayDate)),Integer.parseInt (currentYear.format(todayDate)));
DatePickerDioalg.show();
}
这是XML
<EditText
android:id="@+id/edittext_date"
android:onclick="pickdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.8"
android:background="#FFFF"
android:gravity="center"
android:hint="10-09-2017"
android:textSize="16sp"/>
答案 0 :(得分:2)
为日期选择器
尝试此方法public void pickdate(View button) {
// Get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
edittext_date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
这是时间选择器
public void picktime(View button) {
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
edittext_time.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
timePickerDialog.show();
}
答案 1 :(得分:0)
你应该尝试这个来获取当前日期:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
String todate= dateFormat.format(currentdate());
String finalto = todate.toString(); //here you get current date
private Date currentdate() {
final Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 0);
return cal.getTime();
}