f



Converting java.util.Date to java.sql.Date

Hello Friends
In my program i am using the following codes:

			Date today = new Date();
			log.debug("today is"+today);
			SimpleDateFormat fmt = new SimpleDateFormat("yyyy-mm-dd");
			String todayStr = fmt.format(today);
			log.debug("todayStr value"+todayStr);
			java.sql.Date dt = java.sql.Date.valueOf(new String(todayStr));
			log.debug("current Dt is:"+dt);

The output iam getting is like this:
today is Sat Sep 09 19:20:13 IST 2006(from java.util.Date)
todayStr value 2006-20-09(from string value)
current Dt is: 2007-08-09(from java.sql.Date)


So how to avoid this.Please help me
Thank You in advance
sushovan

0
9/9/2006 2:43:49 PM
comp.lang.java.programmer 52714 articles. 1 followers. Post Follow

2 Replies
589 Views

Similar Articles

[PageSpeed] 54

<mail4sushovan@gmail.com> wrote:
> In my program i am using the following codes:
>
> Date today = new Date();
> log.debug("today is"+today);
> SimpleDateFormat fmt = new SimpleDateFormat("yyyy-mm-dd");
> String todayStr = fmt.format(today);
> log.debug("todayStr value"+todayStr);
> java.sql.Date dt = java.sql.Date.valueOf(new String(todayStr));
> log.debug("current Dt is:"+dt);
>
> The output iam getting is like this:
> today is Sat Sep 09 19:20:13 IST 2006(from java.util.Date)
> todayStr value 2006-20-09(from string value)
> current Dt is: 2007-08-09(from java.sql.Date)
>
>
> So how to avoid this.Please help me

java.util.Date today = new java.util.Date();
long t = today.getTime();
java.sql.Date dt = new java.sql.Date(t);

See also the API doc of java.sql.Date, especially its constructor.
-- 
Thomas 


0
9/9/2006 3:27:20 PM
mail4sushovan@gmail.com wrote:
> Hello Friends
> In my program i am using the following codes:
> 
> 			Date today = new Date();
> 			log.debug("today is"+today);
> 			SimpleDateFormat fmt = new SimpleDateFormat("yyyy-mm-dd");
> 			String todayStr = fmt.format(today);
> 			log.debug("todayStr value"+todayStr);
> 			java.sql.Date dt = java.sql.Date.valueOf(new String(todayStr));
> 			log.debug("current Dt is:"+dt);
> 
> The output iam getting is like this:
> today is Sat Sep 09 19:20:13 IST 2006(from java.util.Date)
> todayStr value 2006-20-09(from string value)
> current Dt is: 2007-08-09(from java.sql.Date)

As shown by Thomas there is an easier and better way to get an 
java.sql.Date from java.util.Date.

In your code example you used the wrong date format: 'mm' stands for the 
minutes of the time part, 'MM' is the format for the month of the date.
0
frank18 (92)
9/9/2006 6:04:09 PM
Reply: