aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Date Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Date Objects" Watch "Date Objects" New topic
Author

Date Objects

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
have a look at the foll. code:
import java.util.*;
class CJdt14
{
public static void main(String a[])
{
String s1= new String("a");
String s2 = new String("a");
Date d1= new Date();

Date d2 = new Date(95,2,14);
Date d3 = new Date(95,2,14);
Date d4 = new Date();
System.out.println(s1.equals(s2));
System.out.println(d2.equals(d3));
System.out.println(d1.equals(d4));
}
}
The o/p : true,true,false.why is it false for the 3 statement.
Thanks

thomas
Ranch Hand

Joined: May 26, 2002
Posts: 79
The no argument constructor creates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond. The equals method compares the time value contained in the Date object. This will be different for d1 and d4 since they were created at different times. Hence the result.
Milind Kulkarni
Ranch Hand

Joined: Jun 01, 2000
Posts: 146
Hi,
When JVM executes Date d = new Date() statement it initializes Date object so that it represents the time at which it was allocated, measured to the nearest millisecond. There is a tiny time gap between creation of 2 Date objects and hence they are initialized to 2 different values. So the result is false.
You can check this using the following program -
import java.util.*;
class CJdt14
{
public static void main(String a[])
{
String s1= new String("a");
String s2 = new String("a");
Date d1= new Date();
Date d2 = new Date(95,2,14);
Date d3 = new Date(95,2,14);
Date d4 = new Date();
System.out.println(s1.equals(s2));
System.out.println(d2.equals(d3));
System.out.println(d1.equals(d4));
System.out.println (d4.getTime());
System.out.println (d1.getTime());
}
}
Hope this helps !!
Regards,
Milind
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks for the reply.I got the point.
But when i modified the program a little bit asimport java.util.*;
class CJdt14
{
public static void main(String a[])
{
String s1= new String("a");
String s2 = new String("a");

Date d1= new Date();
Date d2 = new Date(95,2,14);
Date d3 = new Date(95,2,14);
Date d4 = new Date();
Date d5 = new Date();
System.out.println(s1.equals(s2));
System.out.println(d2.equals(d3));
System.out.println(d1.equals(d4));
System.out.println(d4.equals(d5));
System.out.println(d1.equals(d5));
}
}
I get o/p as true,true,false,true,false at one time.
And when i run it again, i am getting as true,true,true,true,true
i.e d1 and d4 are same.I have seen the o/p using getTime.
but i would be glad if i know why the o/p is not constant.
Thanks in advance.


Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by avn:
Thanks for the reply.I got the point.
But when i modified the program a little bit asimport java.util.*;
class CJdt14
{
public static void main(String a[])
{
String s1= new String("a");
String s2 = new String("a");

Date d1= new Date();
Date d2 = new Date(95,2,14);
Date d3 = new Date(95,2,14);
Date d4 = new Date();
Date d5 = new Date();
System.out.println(s1.equals(s2));
System.out.println(d2.equals(d3));
System.out.println(d1.equals(d4));
System.out.println(d4.equals(d5));
System.out.println(d1.equals(d5));
}
}
I get o/p as true,true,false,true,false at one time.
And when i run it again, i am getting as true,true,true,true,true
i.e d1 and d4 are same.I have seen the o/p using getTime.
but i would be glad if i know why the o/p is not constant.
Thanks in advance.


Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
This is a reliable output from your program and probably most machines today. Date objects are created to the nearest millisec (1/1000) of a second. CPU's running at 300MHz are able to perform many calculations within 1 millisec. It could just as relaibly return false for
System.out.println(d4.equals(d5));
if by chance the creation of these objects is seperated by the rollover of a millisec.

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date Objects