2016-04-12 6 views
0

Sqliteデータベースに日付を挿入したいのですが、この時点で固まっています。私は自分のpojoクラスでDateデータ型を取りました。今私はcontentvalueを使用して私のSqlite Databseにそのものを挿入したいので、どうすればよいですか?それはこのようなものです。SqLiteデータベースに日付を挿入する方法android

SQLiteDatabase db = getWritableDatabase(); 
    ContentValues cv = new ContentValues(); 
    String a = String.valueOf(dp.getDob()); 
    cv.put(DOB, a); 
POJOクラスで

Date dob; 

public DoctorPojo(String name,Date dob,String gender, String adder,String city, 
        String cas,String mobile, Date date,String disease) 
{ 
    this.name = name; 
    this.dob = dob; 
    this.gender = gender; 
    this.adder = adder; 
    this.city = city; 
    this.cas = cas; 
    this.mobile = mobile; 
    this.date = date; 
    this.disease = disease; 
} 
+0

http://tips.androidhive.info/2013/10/android-insert-datetime-value-in-sqlite-database/ –

+0

の可能性のある重複したが、今私は私の作成文の中で取るために何が必要です日付? –

答えて

1

Sqliteをspecficication https://www.sqlite.org/lang_datefunc.htmlによると、あなたはフォーマットYYYY-MM-DDとの文字列として日付を保存する必要があります。

SimpleDateFormatterで簡単に行うことができます。

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
String formattedDate = formatter.format(dp.getDob()); 
cv.put(DOB, formattedDate); 
+0

ありがとうございます...それは私のために働く.. :) –

関連する問題