1

I have a simple todo app that where each 'todo' has a title, description, done status, created date and modified date.

Todo.java

package com.anna.todos.models;

import java.util.Date;

public class Todo {
    private long id;
    private String title;
    private String text;
    private boolean done;
    private Date createdDate;
    private Date modifiedDate;

    public Todo() {
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public boolean isDone() {
        return done;
    }

    public void setDone(boolean done) {
        this.done = done;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public Date getModifiedDate() {
        return modifiedDate;
    }

    public void setModifiedDate(Date modifiedDate) {
        this.modifiedDate = modifiedDate;
    }
}

The created date and modified date are stored in the SQLite database as integers. When I get them from the database I want to be able to correctly convert them to Date objects and when I go to save a todo, I would like to be able to get the integer or long equivalents of those Date objects. (This was done as per the suggestions from another StackOverflow post).

The following is the code that I used to get the dates from the database:

    createdDate = new Date(cursor.getInt(4) * 1000);
    modifiedDate = new Date(cursor.getInt(5) * 1000);

And to save the dates....

    createdDate = Calendar.getInstance().getTime();
    values.put(DatabaseHelper.TODO_CREATED_DATE, createdDate.getTime() * 1000);

My problem now is that the created and modified dates are not being correctly converted. Below are the debugging statements for loading the dates from the database:

04-29 09:21:35.649: I/TodoEditActivity(3954): populateFields() => createdDate=01 Jan 1970 10:33:01 04-29 09:21:35.649: I/TodoEditActivity(3954): populateFields() => modifiedDate=01 Jan 1970 10:34:19

And the debugging statements for saving the dates:

04-29 09:22:00.653: I/TodoEditActivity(3954): saveTodo() => createdDate=01 Jan 1970 10:33:01 04-29 09:22:00.653: I/TodoEditActivity(3954): saveTodo() => modifiedDate=29 Apr 2012 09:22:00

Is there anything that I should do to fix this?

Community
  • 1
  • 1
Anna Lam
  • 777
  • 2
  • 11
  • 28

1 Answers1

0

I suspect the value for createdDate stored in the database is 0 (or a small value). Try setting a variable to cursor.getInt(4) and debug it so you can verify the value gives you a current date. You shouldn't need to multiply by 1000 if you use Date.getTime() or Calendar.getTimeInMillis().

John J Smith
  • 11,435
  • 9
  • 53
  • 72
  • You still can. Store the dates as longs, as you are currently doing and convert them back into dates when you need them. You can can then do calculations using the longs or the dates - whichever you are most comfortable with. – John J Smith Apr 29 '12 at 08:28