Link to home
Start Free TrialLog in
Avatar of vaibhavmishra062201
vaibhavmishra062201

asked on

date string with actual date comparing

Hi,

I am using j2sdk1.4.2
I am getting a date as string in dd/MM/yy format
I am having another date as java.sql.Date

I want to check whether both are equal or not.
Please  check the following code and tell me why dates are not equal during first comparison ?
If you can suggest a better fool proof way to do it, it will be appreciated.
=======================
import java.text.*;
import java.util.*;

public class DateTest
{
      public static void main(String[] args)  throws Exception
      {
            java.sql.Date dateSQL=new java.sql.Date(System.currentTimeMillis());

            //////////////////////////////////////////////////////////
            //replace with today date string in dd/MM/yy format
            String todayDate="19/08/04";

            SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yy");
            java.util.Date dateUtil=sdf.parse(todayDate);
            long l=dateUtil.getTime();
            java.sql.Date dateSQL2=new java.sql.Date(l);

            System.out.println(dateSQL);
            System.out.println(dateSQL2);

            if(dateSQL.equals(dateSQL2))
                  System.out.println("equal");
            else
                  System.out.println("unequal");

            StringTokenizer stk1=new StringTokenizer(dateSQL.toString(),"-");
            int year1=Integer.parseInt(stk1.nextToken());
            int month1=Integer.parseInt(stk1.nextToken());
            int day1=Integer.parseInt(stk1.nextToken());

            StringTokenizer stk2=new StringTokenizer(dateSQL2.toString(),"-");
            int year2=Integer.parseInt(stk2.nextToken());
            int month2=Integer.parseInt(stk2.nextToken());
            int day2=Integer.parseInt(stk2.nextToken());

            if(year1==year2 && month1==month2 && day1==day2)
                  System.out.println("equal");
            else
                  System.out.println("unequal");
      }
}
===========================
vaibhav
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

because they are different classes...

compare the getTime(), not the objects

   if(dateSQL.equals(dateSQL2))

should be

    if( dateSQL.getTime() == dateSQL2.getTime() )

one of them is a java.util.Date, the other is a java.sql.Date
Hi vaibhavmishra,

they are unequal because you create them at different time.

Regards

> java.sql.Date dateSQL=new java.sql.Date(System.currentTimeMillis());

>           //////////////////////////////////////////////////////////
>           //replace with today date string in dd/MM/yy format
>           String todayDate="19/08/04";

>           SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yy");
>           java.util.Date dateUtil=sdf.parse(todayDate);
>           long l=dateUtil.getTime();
>           java.sql.Date dateSQL2=new java.sql.Date(l);

You create the second date a few milliseconds later.
Tim it shouldn't matter since sql Date inherits from util Date. The equals method compares their time anyway.
> You create the second date a few milliseconds later.

Actually no, you create the second date on the midnight on 19/08/2004 while you create the first date right now.
> You create the second date a few milliseconds later.

Ooooh yeah...missed that :-)

>  Tim it shouldn't matter since sql Date inherits from util Date.

I forgot that :-(  *blush*

Wish I hadn't posted here now ;-) hehehehe

/me runs off and hides

;-)
Avatar of cjjclifford
cjjclifford


Hi,

Its not to do with when they are created, as the second is created with the milliseconds from the first...

The latter (i.e. java.sql.Date) rounds off some time information....

Check the hour/minute/second values on the data created with the SimpleDateFormat, and then compare this with that of the java.sql.Date
(simply do a System.out.println() on both Date objects!)

quoting from JavaDoc for java.sql.Date:

To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular  time zone with which the instance is associated.

Cheers,
C
so....what girionis said?
> Its not to do with when they are created, as the second is created with
> the milliseconds from the first...

Actually this is not true as the second is created from todays date which defaults to 19/08/2004 00:00:00 since it is not given the times.

Tim I always do that (forgeting methods and reading questions too fast) hehe :)
Avatar of vaibhavmishra062201

ASKER

hi

thanks for ur comments but i am confused
uptil now i only knew that java.sql.Date contains day/montht/year part and no hour/minutes ... parts
as it also displays only dd/MMyy parts while doing anything with it

if it contains time part also then definately they must be unequal
are you sure of this ?

if yes, can you suggest any way i can compare the date string in dd/MM/yy to a java.sql.Date
other than the way i compared using StringTokenizer

vaibhav
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

girionis, the second object is created with the milliseconds of the first date object, not created with the default constructor, so, it is not different because it is created later!

>           long l=dateUtil.getTime();
>           java.sql.Date dateSQL2=new java.sql.Date(l);

this is a longer version of "java.sql.Date dateSQL2 = new java.sqlDate( dateUtil.getTime() )", albeit "dateUtil" is a strange name for an instance of Date...
I think we have confused the objects. As first object I mean dateSQL, as second object I mean dateSQL2.
indeed we are, many appologies (I really need a holliday!!! starting to see things here ;-( )
thanx girinois, you should also check 2nd condition -
===============================
//unequal
if(dateSQL.getTime() - dateSQL2.getTime() > millisecondsInADay
    && dateSQL.getTime() - dateSQL2.getTime() < 0)
===============================

btw i have opted for a different way -
========================
if(dateSQL.getString().equals(dateSQL2.getString()))
    System.out.println("equal");
else
    System.out.println("unequal");
========================
ne comments about that ?
> if(dateSQL.getString().equals(dateSQL2.getString()))

It's the same thing you can use either approach. But as long as you are happy then it's fine. It does not really matter what approach you will use to solve a problem. I always advocate the "make it work first and optimize later" dogma.