• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Date Objects

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic