Home:ALL Converter>Best way to store, retrieve, and compare dates in Java

Best way to store, retrieve, and compare dates in Java

Ask Time:2012-12-22T23:16:23         Author:scarhand

Json Formatter

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.

Author:scarhand,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/14003948/best-way-to-store-retrieve-and-compare-dates-in-java
Mattias Isegran Bergander :

Just use a primitive long. The same one returned from System.currentTimeMillis(); (for the current time).\nIt 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.\n\nA 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\n\nYou can then compare times just as in your example\n\nlong time1 = System.currentTimeMillis();\nlong time2 = ...;\n\nif (time2 < time1) {\n",
2012-12-22T15:18:08
Hunter McMillen :

You can get a unix timestamp without even creating a Date object if you want to:\n\nlong unixTime = System.currentTimeMillis() / 1000L;\n\n\nThen you can simply write this to MySQL, like you are used to.",
2012-12-22T15:18:20
Natix :

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.\n\nWhen storing into the DB, you obtain the millisecond count (since 1/1/1970) from a java.util.Date with:\n\nDate now = new Date();\nlong millis = now.getTime();\n\n\nAnd then you easily recreate a Date with the given millis with:\n\nlong millis = ...\nDate date = new Date(millis);\n",
2012-12-22T15:36:50
yy