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.
Get Current Date-Time Elsewhere
If you cannot trust the current date-time on the user’s device/computer, then you have two alternatives:
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.
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").