如何在Android Sqlite中添加日期和时间

Android日期和时间选择器手动插入到sqlite数据库中。如何在Android Sqlite中添加日期和时间

  • 日期选择器

  • 时间选取

回答:

你应该已经搜索了一下张贴在这里,无论如何,如果它帮助之前,

private int mYear; 

private int mMonth;

private int mDay;

private int mHour;

private int mMinute;

private Calendar mCalendar;

private void datePicker() {

mCalendar = Calendar.getInstance();

mYear = mCalendar.get(Calendar.YEAR);

mMonth = mCalendar.get(Calendar.MONTH);

mDay = mCalendar.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) {

mCalendar.set(Calendar.YEAR, year);

mCalendar.set(Calendar.MONTH, monthOfYear);

mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

//here you request timePicker Dialog

timePicker();

}

}, mYear, mMonth, mDay);

datePickerDialog.show();

}

private void timePicker() {

mHour = mCalendar.get(Calendar.HOUR_OF_DAY);

mMinute = mCalendar.get(Calendar.MINUTE);

TimePickerDialog mTimePicker = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {

@Override

public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {

// here you go, you got selectedHour and selectedMinute, convert them into String

mCalendar.set(Calendar.HOUR_OF_DAY, selectedHour);

mCalendar.set(Calendar.MINUTE, selectedMinute);

// Now if you want current time in long format

long currentTime= mCalendar.getCurrentTimeInMilis();

// now you can format this to dd-MM using SimpleDateFormat..

System.out.println(formateDate(curentTime));

}

}, mHour, mMinute, false);

mTimePicker.setTitle("Select Time");

mTimePicker.show();

}

//转换为适当的日期格式

public static String formatDate(long dateInMillis) { 

DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy hh:mm a", Locale.getDefault());

Date date = new Date(dateInMillis);

return dateFormat.format(date);

}

就是这样。

以上是 如何在Android Sqlite中添加日期和时间 的全部内容, 来源链接: utcz.com/qa/260871.html

回到顶部