Monday, November 14, 2005

Timestamp bugs..

I hit upon two JDK 1.5 bugs today both related to Timestamp.

  1. Till JDK 1.4.x, Timestamp.compareTo(Date) worked. With JDK1.5, it results into ClasscastException. Since Timestamp extends Date, the caller can always call date1.compareTo(date2) where date1 and date2 can be date or any of its subclass. Now with JDK1.5, if date1 is Timestamp and date2 is Date, it will result into ClassCastException. The code should have always been backward compatible. Try the code below

    Date date1 = new Date ();
    Date date2 = new Timestamp(System.currentTimeMillis());
    int result = date2.compareTo(date1);

  2. In some special cases, two different dates differing only in millisecond values are treated to be equal. You don't believe it?
    Lets take the code sample below, where you create date1 using the value only till second, and compare that with a timestamp date2 that was created with the entire value. Since date1 does not contain millisecond part, timestamp should be greater than date here but date.compareTo() treats them equal.

    long millis = System.currentTimeMillis();
    Date date1 = new Date(millis/1000*1000);
    Timestamp date2 = new Timestamp(millis);
    int i = date1.compareTo(date2);

    This used to work till JDK1.4. It got broken in JDK 1.5 because date now uses getMillis() to compare the time instead of comparing getTime() values. This change must have been made with good intention as getMillis() didn't need any computation and they would have thought that it will become faster. However Timestamp played spilsport here. When you create Timestamp object passing long millisecond value, it breaks it up in two parts - 'millis' in which it stores values till second and 'nanos' in which it stores everything after second. The Timestamp constructior looks like

    public Timestamp(long time) {
    super((time/1000)*1000);
    nanos = (int)((time%1000) * 1000000);
    if (nanos < 0) {
    nanos = 1000000000 + nanos;
    super.setTime(((time/1000)-1)*1000);
    }
    }

    Thus the millis value in this object will be only till second. hence the comparison of the two dates above will make them equal.

The lesson is that don't use compareTo() with dates. Write your own comparator for it in which you compare using date.getTime() :)

4 comments:

Anonymous said...

salute!
I bookmark your site, best greetings.
We enjoyed visiting your website very much. We enjoyed visiting your website very much.

Another links here:

http://ativan.butkel1.org/ ativan

Buy!

Ahamad said...

Good Catch

-ahamad
Software Testing Blog
CFPDF Blog

Anonymous said...

I just found the website who reviews about
Several
home business ideas

If you want to know more here it is
home based business
www.home-businessreviews.com

Anonymous said...

[url=http://tinyurl.com/getvpn][b]Click here to get VPN service![/b][/url]

[b]Anonymous surfing[/b]
Using our service you'll be fully anonymous in the Internet. Hide your IP address, and nobody will know that strange visitor from Germany ( Great Britain, Estonia and so ), is you.

[b]Full access to network[/b]
You can use any services, access any sites and use any software with us. BitTorrent, Skype, Facebook, MySpace, Twitter, Pocker .. we have no restrictions.

[b]Traffic protection[/b]
Don't worry, from this moment all you data will be protected using 256 bit Blowfish encryption algorithm. Nobody can access your internet data.

[b]Wide variety of countries[/b]
You can choose one of over twenty high speed servers located in different parts of the world, from South America coast to islands in Indian Ocean.

Related keywords:
anonymous surfing review
proxy server vpn
anonymous secure surfing
proxy vpn
anonymous vpn free
internet explorer vpn
vpn dial up
ssl vpn
Traffic protection
anonymous surfing freeware
anonymous surfing software
vtunnel
anonymous surfing vpn
best anonymous browser
surf the web anonymous
best anonymous surfing
anonymizer anonymous surfing review
firefox anonymous surfing
Virtual Private Networks
Free Vpn Client Software
anonymous surfing software
[url=http://dasbmw.ru] anonymous surfing software[/url]
[url=http://seobraincenter.ru] anonymous proxy[/url]