Friday, May 1, 2009

Java : Converting java.util.Date to java.sql.Date

Many a times we need to insert the date of transaction in the database, but the problem is that the PreparedStatement Implementations have a setDate() method which takes java.sql.Date as an argument and same is the case with the ResultSet which is returned from the database as java.sql.Date is an equivalent to Date datatype in the DB.

But in java we can get current date from a new Object of java.util.Date, so here is how to convert it into java.sql.Date

java.util.Date today = new java.util.Date();

java.sql.Date date = new java.sql.Date(today .getTime());

java.sql.Date only saves the date though and not other attributes like day, time, timezone etc.

If the field in the Database is defined as DATETIME then we can use java.sql.Timestamp instead.

java.sql.Timestamp ts = new java.sql.Timestamp(today .getTime());

here is the result

System.out.println(today); ---> Fri May 01 17:54:26 IST 2009 (util.Date)
System.out.println(date); ---> 2009-05-01 (sql.Date)
System.out.println(ts); ---> 2009-05-01 17:54:26.199 (sql.Timestamp)

Timestamp doesn't hold timezone and day attributes.

Thats it for now folks.

LazyCoder
Signing off for now...



Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

Bachachans showing middle finger after casting their vote

May Day being a holiday I lazily woke up at 9am, opened the door of my home to pick up the newspaper and there it was right on the front page, the Bachchans posing with their middle finger after casting their votes.

Bachchans


I was taken aback but still couldn't help giggling and was quite curious as to why the ink was applied to their middle finders instead of the index finger, right besides their photo, was a picture of Aamir Khan with his middle finger also inked.

I just got curious and performed a quick search only to find this article on TOI's website

Voters will now show middle finger

That sounds absurd and watching the celebrities posing in such a fashion is tasteless indeed.

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

Tuesday, April 28, 2009

Java Example : How To Swap Integer and String Variables Without Using a Temporary Variable

Hi guys,

Swapping two variable has always been a classic learning example for beginners, well for a change in the example below we would be learning to swap two variables (String or Integer) without using a temporary variable.

/*

  *   @(#)Swap.java       1.1   28/04/09

  *

  *   Copyright   2009   Lazy   Coder.   All   rights   reserved.

  *   To   be   used   only   for   educational   purposes.

  *   Not   to   be   reproduced   and   published

  *   without   seeking   prior   consent.

  */


package   com.lazy.coder.examples;





/**

  *   A   class   that   demonstrates   how   to   swap  

  *   two   strings   or   two   integers   without

  *   using   a   temporary   variable

  *  

  *   @author   lazycoder

  *  

  */


public   class   Swap   {



      

       /**

         *   @param   args

         */


       public   static   void   main(String[]   args)   {

              //Swapping   two   String   without   using   temp   variable.

              String   firstString   =   "Lazy";

              String   secondString   =   "LazyCoder";

              System.out.println("firstString   =   "   +   firstString);

              System.out.println("secondString   =   "   +   secondString);

              System.out.println("Swapping");

              firstString   =   firstString   +   secondString;

              secondString   =   firstString.substring(0,   firstString.length()

                            -   secondString.length());

              firstString   =   firstString.substring(secondString.length());

              System.out.println("firstString   =   "   +   firstString);

              System.out.println("secondString   =   "   +   secondString);

             

              //Swapping   two   Integers   without   using   temp   variable.

              int   firstInt   =   111111111;

              int   secondInt   =   999999999;

              System.out.println("firstInt   =   "   +   firstInt);

              System.out.println("secondInt   =   "   +   secondInt);

              System.out.println("Swapping");

              /*

                *   Using   exclusive-or   (XOR)   to   avoid  

                *   stack   overflow/underflow

                */


              firstInt   =   firstInt   ^   secondInt;

              secondInt   =   firstInt   ^   secondInt;

              firstInt   =   firstInt   ^   secondInt;

              System.out.println("firstInt   =   "   +   firstInt);

              System.out.println("secondInt   =   "   +   secondInt);

       }

}



Thats all for now, check back for more java examples.

Lazy Coder
signing off for now...

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

Monday, April 27, 2009

Adding two Object arrays or Collections in Java

Hi guys,
I was feeling a little bit bored so thought of posting a java example on my blog. Starting with a basic one for now, many of the java newbies have faced problems while trying to combine two Object arrays or collections in java. We will learn just how to do that in this post.

PS: I have used the extended for loop in the example below and so it requires JDK 5 or above to run.

The code below will not hold duplicates in the resultant; if you want to hold duplicates as well then define alist as ArrayList instead of a HashSet.

/*

  *   @(#)CombiningCollections.java       1.1   27/04/09

  *

  *   Copyright   2009   Lazy   Coder.   All   rights   reserved.

  *   To   be   used   only   for   educational   purposes.

  *   Not   to   be   reproduced   and   published

  *   without   seeking   prior   consent.

  */


package   com.lazy.coder.examples;



import   java.util.Arrays;

import   java.util.HashSet;



/**

  *   @author   lazycoder

  *  

  */


public   class   CombiningCollections   {



       /**

         *   A   method   which   takes   two   collections

         *   as   parameters   and   returns   a   resultant

         *  

         *   @param   collection1

         *   @param   collection2

         *   @return

         */


       public   Object[]   combineCollections(Object[]   collection1,

                     Object[]   collection2)   {

              HashSet   alist   =   new   HashSet();

              for   (Object   obj   :   collection1)

                     alist.add(obj);

              for   (Object   obj   :   collection2)

                     alist.add(obj);

              Object[]   collection   =   alist.toArray();

              /*

                *   Used   to   sort   the   elements   in   ascending   order

                *   all   elements   in   the   array   must   be   mutually

                *   comparable(that   is,   e1.compareTo(e2)   must   not  

                *   throw   aClassCastException   for   any   elements  

                *   e1   and   e2   in   the   array).

                */


              Arrays.sort(collection);

              return   collection;

       }



       /**

         *   @param   args

         */


       public   static   void   main(String[]   args)   {

              Object[]   collection1   =   {   "A",   "C",   "1",   "D"   };

              Object[]   collection2   =   {   "Bits",   "Nibble",   "Bank"   };

              CombiningCollections   combine   =  

                     new   CombiningCollections();

              Object[]   collection   =  

                     combine.combineCollections(collection1,

                            collection2);

              for   (Object   obj   :   collection)

                     System.out.println(obj);

       }

}



Hope you guys will find this helpful.

Lazy Coder
Signing off for now....

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

Sunday, April 26, 2009

Roadies Hell Down Under Concludes

I think there are many more like me who would glue themselves to their couches and bean bags with their Tv sets turned on during weekends just to watch youngsters abuse each other and fight besides performing tasks.

Yes, I am talking about fans of reality shows Splitsvilla 2 and Roadies Hell Down under 6.0, Raghu and Rannvijay became celebrities with these shows becoming an instant hit.

To cut to the chase, today the final episode of Roadie hell down under was telecasted in India, and must say I was very much disappointed after watching it. With Sufi, already losing the wildcard entry to the finals I wasn't much interested but then again carried on just to watch cute babes like Tammana, Roop and Bobby once again.

Kiri Timung
Nauman SaitPalak

Kiri, Nauman and Palak were the three roadies who ended up in the final episode, I wasn't taking any sides and din't care much as to who would win. Kiri won the first task which was some form of rock climbing and had the power to pick a roadie besides him for the final showdown.

But the devils Raghu and Rajiv are they confused him and Kiri made a dumb choice by picking up Nauman for the final task and ultimately losing to him in the last task.

Guess thats a decision he will keep on regretting upon; and finally we ended up by having a Roadie like Nauman who doesn't even have a back bone who couldnt perform the tasks with elan.

Well I guess its luck after all that matters with skill and hard work... As for Palak I am sure she must be crying somewhere.

All in all I didn't find this season better than the previous ones and IMHO the tasks all sucked mostly comprising of sports like rugby, ice hockey etc.

Lazy coder signing off for now....

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl