Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date Objects

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic