user949884
user949884

Reputation: 503

I get ever a false date in SqLite

I save a Date and this is ever 1698-19-20 and not 2012-4-12. Here one sees the error? Date saved as Integer.

calculated with the calculator: 1334262386066 millisecond

42.309182713914 year + 1970 Year = ~2012

Code:

Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
calendarEntity.setDate(date);

sqlite query:

SELECT strftime('%Y-%m-%d',DATE) FROM CALENDAR_ENTITY

Table:

  CREATE TABLE 'CALENDAR_ENTITY' ('_id' INTEGER PRIMARY KEY ,'TITLE' TEXT,'NOTICE'        TEXT,'DATE' INTEGER,'BABY_ID' INTEGER NOT NULL )

Upvotes: 0

Views: 448

Answers (2)

Tomik
Tomik

Reputation: 23977

The problem is that strftime function expects the date as text in one of these formats:

YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDD.DDDD

You are storing the date as integer which strftime function can't handle. More details about SQLite datetime functions here (or here, page 80).

Upvotes: 1

mbaydar
mbaydar

Reputation: 1194

If you can use this return long like this I think your problem will solved itself.

Date date = new Date(1334262386066l); 

This will return what you expected. And this is what it prints

Thu Apr 12 23:26:26 EEST 2012

Upvotes: 1

Related Questions