Java Q & A Blog

February 27, 2008

How to convert java.util.Date , java.sql.Date , java.sql.Time , java.sql.Timestamp

Filed under: Developer — Tags: — Devesh S N Bhatta @ 6:45 pm



Simple Utility Class but Memory consuming



class DateUtil{

/**
* Convert the java.util.Date to java.sql.Date.
* @param utilDate the time.

* @return the converted time.
*/

private static final java.sql.Date utilDateToSqlDate(java.util.Date utilDate) {
return new java.sql.Date(utilDate.getTime());
}
/**
* Convert the java.util.Date to java.sql.Time.
* @param utilDate the time.
* @return the converted time.
*/

private static final java.sql.Time utilDateToSqlTime(java.util.Date utilDate) {
return new java.sql.Time(utilDate.getTime());
}


/**
* Convert the java.util.Date to java.sql.Timestamp.
* @param utilDate the time.
* @return the converted time.
*/


private static final java.sql.Timestamp utilDateToSqlTimestamp(java.util.Date utilDate) {
return new java.sql.Timestamp(utilDate.getTime());
}


Complex Utility Class but Memory Efficient

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
class DateUtil{
private static final DateFormat utilDateFormatter = new SimpleDateFormat("dd-MM-yyyy");
private static final DateFormat sqlDateFormatter = new SimpleDateFormat("yyyy-MM-dd");


public static java.sql.Date utilDateToSqlDate(java.util.Date uDate) throws ParseException
{
return java.sql.Date.valueOf(sqlDateFormatter.format(uDate));
}


public static java.util.Date sqlDateToutilDate(java.sql.Date sDate) throws ParseException
{
return (java.util.Date)utilDateFormatter.parse(utilDateFormatter.format(sDate));
}

}

About these ads

10 Comments »

  1. Thanks for your useful help

    Comment by Fernando Urea — October 16, 2008 @ 6:32 am

  2. I was getting Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff format error in java. these methods helped me in resolving.

    thanks a lot

    Comment by sunitha — January 22, 2009 @ 7:37 am

  3. Shouldn’t that be:

    public static java.util.Date sqlDateToutilDate(java.sql.Date sDate) throws ParseException {
    return sqlDateFormatter.parse(sDate.toString()));
    }

    Comment by William — July 10, 2009 @ 5:15 pm

  4. Thanks a zillion dude, you saved me. Thanks!

    Good work. Keep up!

    Comment by John Dondpati — August 18, 2009 @ 3:03 am

  5. Thanks a lot. This is awesome.

    Comment by Sandeep — May 8, 2010 @ 6:50 am

  6. what i was specifically looking for.
    saved time..
    Thanks

    Comment by ocpjp — December 17, 2010 @ 9:27 pm

  7. Special Thank ^^

    Comment by Thanh Tung — April 7, 2011 @ 4:55 pm

  8. can anyone say me how to use this code ..

    Comment by Anandhan — October 31, 2011 @ 10:38 am

  9. It’s really surprised why Java does not have standard methods for all these conversion. Everytime i had to convert i just have google for the sample code.

    Comment by jguide — May 11, 2012 @ 5:37 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: