David
David

Reputation: 109

How to retrieve formated date field in sqlite in my android code?

I have following code in my android program:

Cursor cursor = db.rawQuery("select ID,firstname,date(birthday) from user", null);  
String birthdaystr=cursor.getString(2);

But the value of birthdaystr is null.

I changed my code to:

Cursor cursor = db.rawQuery("select ID,firstname,birthday from user", null);  
String birthdaystr=cursor.getString(2);

The value of birthdaystr looks like "Wed Jan 10 00:00:00 +0000 1973".

Anybody know this issue? (It seems I need to get the value by another way if I use date() function)

Upvotes: 0

Views: 1302

Answers (3)

Chirag
Chirag

Reputation: 56925

Use strftime function to retrieve formated date.

strftime(" + "\"%Y" + "-" + "%m" + "-" + "%d\"" + ",birthday)

or Use

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formatted = sdf.format(new Date(birthdaystr));

You can apply any date format what you want.

Upvotes: 1

Name is Nilay
Name is Nilay

Reputation: 2783

Why dont u try:

Cursor cursor = db.rawQuery("select ID,firstname,birthday from user", null);  
String birthdaystr=cursor.getString(2);
SimpleDateFormat sdf = new SimpleDateFormat("HH:m:ss dd-MM-yyyy");
String TimeStampDB = sdf.format(new Date(birthdaystr)); 

Upvotes: 0

Vyacheslav Shylkin
Vyacheslav Shylkin

Reputation: 9791

Try:

....
Cursor cursor = db.rawQuery("select ID,firstname,birthday from user", null);  
String birthdaystr=cursor.getString(2);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formatted = sdf.format(new Date(birthdaystr));
..... 

Upvotes: 0

Related Questions