I have a Database that when ever i put in information about things i want the database to show a time stamp with the information that i put in.
2 Answers
The simplest option might be to do it manually. Add an integer column and store System.currentTimeMillis()
in that column.
EDIT
If you want to store it human readable create a text
column. Then create the timestamp something like this
SimpleDateFormat sdf = new SimpleDateFormat("d MMM yyyy HH:mm:ss.S z");
Date now = new Date();
String timestamp = sdf.format(now);
which creates a String like this (you can change the format, check out the SimpleDateFormat
documentation)
9 Dec 2011 23:06:05.849 EST
and if you are reading from the database you can do this to get a date object
Date then = null;
try {
then = sdf.parse(timestamp);
} catch (ParseException e) {
e.printStackTrace();
}
If you don't really care about the format you can just do
String timestamp = new Date().toString();
which will give output like
Fri Dec 09 23:06:05 EST 2011

- 9,898
- 5
- 43
- 52
-
Isn't that just going to put the time into milliseconds? – Christian Dec 10 '11 at 03:44
-
Yes, it's a timestamp in milliseconds. If you want a different representation you could use `SimpleDateFormat` and store a text column instead. Depends on whether you want the field to be directly human readable or not – skynet Dec 10 '11 at 03:51
-
Yeah I want it so that the user can look at what time the information was put into the database. so it would be nice if it was human readable. If you had an example of that that would be nice. – Christian Dec 10 '11 at 03:55
I'm assuming when you say "put in information" you're talking about an insert. If that's the case, you can just create a datetime column with the default of now in your schema:
DATETIME DEFAULT CURRENT_TIMESTAMP
You can also set it on your insert:
INSERT INTO your_database VALUES (datetime())
See here for more details: How to insert a SQLite record with a datetime set to 'now' in Android application?
Good luck!

- 1
- 1

- 578
- 1
- 4
- 12