我使用DatePicker并在方法onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth)
中得到年份,月份和dayOfMonth,如何在单个日期中合并这3个int,我可以用它来存储在数据库中?
答案 0 :(得分:0)
实例化日历:
Calendar cal = Calendar.getInstance();
使用它的方法设置日期:
cal.set(int year, int month, int day);
然后将其存储为时间戳整数
cal.getTimeInMillis()
或作为字符串
cal.toString()
答案 1 :(得分:0)
您可以使用String
将其转换为格式为dd/mm/yyyy
的{{1}}。
然后,当您想要检索它时,使用一些SimpleDateFormatter
操作来检索每个单独的组件并构造一个String
对象。
答案 2 :(得分:0)
你可以做这样的事情
Calendar myCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
private void updateLabel()
{
String myFormat = "MM/dd/yy"; //In which format you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
textview.setText(sdf.format(myCalendar.getTime()));
}
希望有所帮助
答案 3 :(得分:0)
试试这个:
private SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
private void datePicker(Date initialDate) {
Calendar cal = Calendar.getInstance();
if (initialDate != null) {
cal.setTime(initialDate);
}
new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String data = String.format("%02d/%02d/%04d", dayOfMonth, monthOfYear + 1, year);
try {
Date newDate = format.parse(data);
saveMyDate(newDate);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)).show();
}
private void saveMyDate(Date newDate) {
// do something
}
答案 4 :(得分:0)
使用日历设置年,月,日
GregorianCalendar cal = new GregorianCalendar();
cal.set(GregorianCalendar.YEAR, year);
cal.set(GregorianCalendar.MONTH, monthOfYear);
cal.set(GregorianCalendar.DAY_OF_MONTH, dayOfMonth);
并以毫秒为单位存储
cal.getTimeInMillis();