How to compare dates while ignoring timezones in Java

2.1k Views Asked by At

I'm storing dates and times as one of the fields in my sql database in the following format: dd/mm/yy hh:mm. I am importing this info into my android app with an asynctask and then populating a listview.

What I need to do is to check whether or not the date imported is after or before the current date and time. All the data in my database is in GMT and I am wondering what method should I call in java to get the best representation of the current date and time independent of the timezone the user is in, so that I can carry out the comparison.

My main concern is to avoid the user being able to cheat the app by changing the system time on his phone.

2

There are 2 best solutions below

0
On BEST ANSWER

Get Current Date-Time Elsewhere

If you cannot trust the current date-time on the user’s device/computer, then you have two alternatives:

  • Get the current date-time from somewhere else.
    • See this Question or this Question for getting current date-time from time servers using Java/Android.
    • See this Question for getting current date-time from your database server.
  • Let your database server use its own date-time when searching for records.
    See Questions like this or this. Pay careful attention to the various date-time functions offered by your particular database. For example, some commands return the moment when the transactions began while others return the current moment of execution. A few commands are standard SQL while most are proprietary. As an example, see the Postgres date-time functions doc.

Joda-Time

If you get the time from elsewhere, then you need to work with the date-time values in Java. When doing such work, use a good date-time library. For Android that means the Joda-Time library. Avoid the bundled java.util.Date/.Calendar as they are notoriously troublesome.

There are many other Questions and Answers on StackOverflow on this topic, but here's a quick untested example. I assume you are using a JDBC driver to obtain a java.sql.Timestamp value from your database.

java.sql.Timestamp ts = myResultSet.getTimestamp( "my_column_" );  // Get date-time object from your database via JDBC driver.
…
DateTime now = … // Get current date-time from other computer as discussed above.
DateTime when = new DateTime( ts , DateTimeZone.UTC );  // Convert java.sql.Timestamp to org.joda.time.DateTime object.
Boolean rowIsPast = when.isBefore( now );  // Compare DateTime objects.

If you are getting string representations of your data’s date-time values from the database, try to instead get an object via JDBC, a java.sql.Timestamp (or in the future, an object of a type defined by the new java.time package in Java 8 and later). If you must work with string representations, search for many examples on StackOverflow.com of parsing strings into date-time values in Java & Joda-Time (use the search term "joda").

6
On
String time1 = "16:00:00";
String time2 = "19:00:00";

SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date date1 = format.parse(time1);
Date date2 = format.parse(time2);
long difference = date2.getTime() - date1.getTime();

This would give you the time difference in seconds. Check to see if that is positive to see which date is "first"