• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

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

Android 来源:user9111896 10次浏览

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); 
} 

就是这样。


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)