如何将日期从DatePicker保存到Android Studio SQLite

问题描述:

我有一个SQLite数据库为我的Android应用程序项目,我想保存从日期选择器的日期。 为了尽可能高效,我无法将该日期作为字符串插入。 所以我想我的日期保存为整数或长,但我检查各地的网站并没有什么清晰,足以帮我...如何将日期从DatePicker保存到Android Studio SQLite

这里是我的代码:

public DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() { 
    @Override 
    public void onDateSet(DatePicker view, int year, int month, int day) { 

     // Do something with the chosen date 
     TextView dateView = (TextView) findViewById(R.id.newdatepicked); 

     // Create a Date variable/object with user chosen date 
     Calendar cal = Calendar.getInstance(); 
     cal.setTimeInMillis(0); 
     cal.set(year, month, day, 0, 0, 0); 

     Date date = cal.getTime(); // CAN I USE THIS LINE? 

     // Format the date using style MEDIUM and FR locale 
     DateFormat df_date = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE); 
     String dateDF= df_date.format(dateDF); 
     // Display the formatted date 
     dateView.setText(dateDF); 

    } 
}; 

而且我的代码插入我所有的数据:

public void SaveAlert(View v) { 

    String Article = tvArticle.getText().toString(); 
    long DateEntree = calendar.getTimeInMillis(); // This line is working very well 
    long DateSortie =     ;///////// I CANNOT FIND OUT HOW TO HANDLE THIS ... 

我的问题是与DateSortie。

请让我为我的英语水平,并感谢您的帮助表示歉意很多

+0

你告诉selevted日期的格式进行检索在textview中。所以只需在数据库中保存dateDF值作为字符串 –

你仍然可以将其存储为字符串,然后使用您想例如

Calendar calendar = Calendar.getInstance(); 

    /* to display time*/ 
    SimpleDateFormat formatter = new SimpleDateFormat("hh:mm"); 

    /* to display date in the given format */ 
    DateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy"); 

    /*get the date using */ 
    Date dDate = dateFormatter.parse(yourObject.getTaskDate()); 

    /*set the date */ 
    calendar.setTime(dDate); 
+0

感谢您的回答,我知道如何用字符串插入它,但我已经读过它更好将其保存为整数以日期排序为例。 –

+0

这可能可以帮助你使用日历转换你选择的日期到时间戳[http://*.com/a/13694823/6213163] –

+0

嗨,@Anirudh,感谢您的帮助,它工作正常;) –