How to compare dates while ignoring timezones in Java

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.

--------------Solutions-------------

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").

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"

Category:java Time:2018-12-01 Views:1

Related post

  • how to compare date coloumn in sqlite where condition 2011-02-08

    how to compare date coloumn in sqlite where condition --------------Solutions------------- It's hard to tell what exactly you're asking, but the SQLite website has a page on date and time functions that should help. If you store your dates as Unix ti

  • how to compare dates 2011-05-10

    Possible Duplicate: How to Compare Dates in php? I have these two dates 1305004066 1305007443 I want to compare which one is later on than other. How can I do that --------------Solutions------------- If it's UNIX timestamps (seconds since 1970-01-01

  • How to pass data while using get method in xdr 2012-03-22

    As IE does not support cross domain issues, we have to use get or post method by using xdr, my problem is, I don't know how to pass data while using get method with xdr. Code snippet for get method using jquery ajax is like - $.ajax({ type: 'GET', ca

  • How to write data to smart card using java card? with example 2011-11-27

    can some one tell me how to write data in smart card using java card? i have this code i am using java card 2.2 private void writeName(APDU apdu) throws ISOException { apdu.setIncomingAndReceive(); byte[] apduBuffer = apdu.getBuffer(); byte j = (byte

  • can some one tell me how to read data in smart card using java card? I have this code i am using java card 2.2 2011-11-27

    I am new to java card. i have this code.....so please tell me how to read data from smart card using java card private void readName(APDU apdu) throws ISOException { byte[] apduBuffer = apdu.getBuffer(); for (byte i=0; i<userName.length; i++) { ap

  • How to sign data with .p12 file in Java? 2011-12-02

    How to sign data with .p12 file in Java? Java seems to support only until pkcs11, I just can't find the standard library for pkcs12. There's nothing in 'java.security'... for decoding .p12 file to get private key to perform signing. Any solution? I'm

  • How to store data in text file in java 2012-01-26

    How to store data in text file in java that has various attributes like name, author etc that will be inputted by the user on CLI. any Idea? Thanks --------------Solutions------------- It sounds like the Java class that will suit you best is a FileWr

  • How to insert data with IGNORE if one variable is allways different? 2011-05-06

    I would like to use INSERT IGNORE INTO in order to enter only data that doesnt exist allready in my database. Now my question is: How can I do this if I have a variable "time" which is allways different. But the rest would be the same. For example: "

  • How to compare just the day in a java date? 2011-06-18

    I'm trying to do a simple date comparison between yesterday and today if (yesterday.before(today)) { ... } The issue is that with the before method I will eval to true even if it's just a few seconds difference. How might I compare just the day (beca

  • how to compare dates in iphone sdk? 2009-11-07

    i have two dates i want to compare which one is greater date how do i do this in objective-c? --------------Solutions------------- From NSDate class reference: NSDate -compare: Returns an NSComparisonResult value that indicates the temporal ordering

  • How to compare data file with column in SQL server database? 2009-11-17

    I am required to compare data in a file with a column in SQL server database. I have been asked to use Microsoft Access to do the same. Additional information: File consists of many lines of text. Amongst the text is the a word that matches the colum

  • How to compare dates in Java? 2010-04-07

    How do I compare dates in between in Java? Example: date1 is 22-02-2010 date2 is 07-04-2010 today date3 is 25-12-2010 date3 is always greater than date1 and date2 is always today. How do I verify if today's date is in between date1 and date 3? ------

  • How to compare dates in Django 2010-09-26

    I would like to compare a date to the current date in Django, preferably in the template, but it is also possible to do before rendering the template. If the date has already passed, I want to say "In the past" while if it is in the future, I want to

  • How to parse date string using Timezone in GWT 2011-02-24

    Has anybody succeeded parsing date string with a custom timezone in GWT? GWT's DateTimeFormat allows to format dates based on time zone, but I haven't found any method for doing opposite operation. So what should I do if I have following string "02:0

  • During migration from oracle to MySQL how to compare data between two using Java 2011-06-07

    I have migrated data from Oracle to MySQL using MySQL Migration Toolkit as POC. Now I want to compare data between same tables of different databases with Java. I have done the simple total no of rows of the tables in two database. But I want to chec

  • How to sort alphabetically while ignoring case sensitive? 2011-09-19

    i have this code, but works only for lower case letters... i want this to sort the list while ignoring the upper case letters.. package sortarray.com; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.B

  • How to load data while scrolling horizontally and on a DIV overflow? 2011-09-30

    I’m working on this horizontal scrollbar that grabs content from the database when you scroll to the farthest right. Here's an example of what we want to do: http://designarchives.aiga.org/#/entries/%2Bcollections%3A%2250%20Books%2F50%20Covers%20of%2

  • How to compare two jsons ignoring order of elements in array properties? 2011-11-11

    I need to compare two strings which represent json objects. For testing purposes I need a way to compare these strings ignoring not only the child elements order (which is quite common) but order of elements in array properties of jsons. I.e.: group:

  • How to compare data in two columns and also save the third column data in the output. 2015-03-06

    This is a query about comparing data in two columns, in an Excel spreadsheet, in which there are 2500 records. In Column A, I have 2500 (or thereabouts) surnames. In Column D, I have 2500 (or thereabouts) surnames and in Column E, I have the correspo

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 3.959 (s). 15 q(s)