日期DatePickerDialog的使用:public void showDateDialog() {
Calendar d = Calendar.getInstance(Locale.CHINA); // 创建一个日历引用d,通过静态方法getInstance() 从指定时区 Locale.CHINA 获得一个日期实例
Date myDate = new Date(); // 创建一个Date实例
d.setTime(myDate); // 设置日历的时间,把一个新建Date实例myDate传入
int year = d.get(Calendar.YEAR); int month = d.get(Calendar.MONTH); int day = d.get(Calendar.DAY_OF_MONTH); //初始化默认日期year, month, day
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new OnDateSetListener() { /**
* 点击确定后,在这个方法中获取年月日
*/
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
date = "" + year + "-" + monthOfYear + "-" + dayOfMonth;
}
},year, month, day);
datePickerDialog.setMessage("请选择日期");
datePickerDialog.show();
}
TimePickerDialog简单使用private void showTimeDialog() { /**
* 0:初始化小时
* 0:初始化分
* true:是否采用24小时制
*/
TimePickerDialog timeDialog = new TimePickerDialog(this, new OnTimeSetListener() { //从这个方法中取得获得的时间
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) { if(hourOfDay<10){
mHour="0"+hourOfDay;
} if(minute<10){
mMinute="0"+minute;
}
}
}, 0, 0, true);
timeDialog.show();
}