Skip to content

Comparing the Timestamp/date between two given timestamps/dates

June 5, 2010

JavaUsing java.util.Date is quite common and often we are encountered with situations where we want to compare Timestamp. The class hierarchy of Timestamp class is:
  java.lang.Object  

            java.util.Date     

                  java.sql.Timestamp  

The code given below represents a situation where current timestamp (today’s date) is between two given timestamps i.e startDate and endDate.  

Function

public int timeStampCompare(Date today,Date date){
            if (today == date) {
                return 0;
            }
            else if (today.compareTo(date)<0) {
                 return -1;
            }
           else{
                return 1;
           }
}

 Calling the above function

Date today = new Date();// gives us the present date and time

           if (timeStampCompare(today, startDate)!=-1 && timeStampCompare(today, endDate)!=1 ){
               System.out.println(“ I am between the timestamp ”);
           }
           else{
               System.out.println(“ Oppss! time is over ”);
           }
About these ads

From → Java

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: