Reputation: 4337
I come from a PHP background. I've been a PHP developer for a long time now. I am wanting to jump into Android App development. I created one app, but I don't feel like I have a strong grasp of the time and date functions, especially when being stored in the database. O one of my main concerns is how to store and retrieve dates from the database (SQLite) and compare them.
The way I do it in PHP is I simple; I use the time()
function, which creates a unix timestamp for the current time, and then I store it as an integer in a MySQL database. This makes things very simple and easy due to MySQL's date functions, as well as the ability to easily retrieve and compare the date via PHP as an integer.
So basically, I'm wondering what the best solution would be for Java. What do you recommend I do when creating, storing, retrieving, and comparing dates for my future Android apps?
Here is an example of the PHP code I'd use, perhaps this will give someone a better idea of what I'm used to and what I'm looking for:
Database structure:
`id` (int) auto increment
`username` (varchar)
`the_date` (int)
<?php
$username = 'scarhand77';
$the_date = time();
// insert into database
mysql_query("insert into `stack_overflow` (`username`, `the_date`) values ('$username', '$the_date')");
// retrieve from database
$user_date = mysql_result(mysql_query("select `the_date` from `stack_overflow` where `username`='$username'"), 0);
// compare $user_date, see if its older than right now
if ($user_date < time())
echo 'it is older';
else
echo 'it is not older';
?>
Any help would be greatly appreciated.
Upvotes: 2
Views: 5976
Reputation: 14257
SQLite supports only a few of datatypes, where the only sensible one for storing time is the integer
. It can hold up to 64-bit numbers, so it works well with Java's long
.
When storing into the DB, you obtain the millisecond count (since 1/1/1970) from a java.util.Date
with:
Date now = new Date();
long millis = now.getTime();
And then you easily recreate a Date with the given millis with:
long millis = ...
Date date = new Date(millis);
Upvotes: 1
Reputation: 55
If you are doing manipulation with the times after you retrieve them from the database you may want to check out Joda Time. It has a huge amount of useful functions whereas the standard Java library is a little lacking in the area.
Upvotes: 0
Reputation: 11909
Just use a primitive long. The same one returned from System.currentTimeMillis();
(for the current time).
It is the number of milliseconds since 1970. It is basically the same as unix time but with a signed 64bit value (java long) and in milliseconds instead of seconds.
A java.util.Date
object is just a wrapper around a long
too, so mostly pointless but easy to use with the Date object if you like too. Not to be confused with java.sql.Date
You can then compare times just as in your example
long time1 = System.currentTimeMillis();
long time2 = ...;
if (time2 < time1) {
Upvotes: 7
Reputation: 61510
You can get a unix timestamp without even creating a Date object if you want to:
long unixTime = System.currentTimeMillis() / 1000L;
Then you can simply write this to MySQL, like you are used to.
Upvotes: 2