k4sia
k4sia

Reputation: 416

How to get date value from SQLite in java

I have a simple table in my database:

CREATE TABLE [InformacjeZDziekanatu] (
  [_id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 
  [DataWstawienia] DATE NOT NULL, 
  [DataModyfikacji] DATE NOT NULL, 
  [Tresc] VARCHAR2 NOT NULL);

In my application only what I want to do is to get value of DataWstawienia column. I am using such method:

try {
    ResultSet result = stat.executeQuery("select * from InformacjeZDziekanatu order by _id desc limit 5");
    int id;
    Date dataWst;
    Date dataMod;
    String tresc;               

    for(int j = 0 ; j < 5 ; j++) {
            result.next();
            Object[] lista = new Object[4];
            id = result.getInt("_id");
            dataWst = result.getDate("DataWstawienia");
            dataMod = result.getDate("DataModyfikacji");
            tresc = result.getString("Tresc");
            lista[0] = id;
            lista[1] = dataWst;
            lista[2] = dataMod;
            lista[3] = tresc;
            dane[j] = lista;

    }
}

All dates in DataWstawienia column are today's dates but using this method above I get 1970-01-01 date all the time.

What did I do wrong?

Upvotes: 1

Views: 2501

Answers (1)

vy32
vy32

Reputation: 29645

SQLIte does not have a DATE data type. Dates need to either be stored as integers (number of seconds since Unix Epoch is common) or as ISO 8601 times (e.g. 2013-10-01T12:12:12). If you say DATE in your SQLite schema you will store values as strings. Your solution of changing the type of dateWst to String acknowledges the fact that you are storing dates as strings and not as an internal date type.

Upvotes: 1

Related Questions