aspose file tools*
The moose likes Java in General and the fly likes how to parse java.util.Date to java.sql.date ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to parse java.util.Date to java.sql.date ?" Watch "how to parse java.util.Date to java.sql.date ?" New topic
Author

how to parse java.util.Date to java.sql.date ?

Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 437
hi, good day, if i have a java.util.Date with date "12/25/2005", how to i parse this date into java.sql.Date ?

thank you
Sachin Dimble
Ranch Hand

Joined: Dec 07, 2005
Posts: 100
Hi Nakata,
Try Following code will get solve ur problem!

Code:

import java.text.SimpleDateFormat;

import java.util.*;

class DateSwap
{

public static void main(String args[])
{
SimpleDateFormat df =
new SimpleDateFormat("EEEE-dd-MMMM-yyyy");
Date d1=new Date();
System.out.println(df.format(d1));

}
}

With Best Regards,
Sachin Dimble
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Your use of the word "parse" here is problematic, because it means a conversion from a String, but you want to go from java.util.Date to java.sql.Date, eh? I would pass the first the first date's long time value (date.getTime()) to the latter's constructor, but read what the java.sql.Date API says about normalizing the time.


There is no emoticon for what I am feeling!
Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 437
thanks Sachin Dimble and Jeff Albrechtsen

my concern is more on how to pass the util.date into sql.Date

i have following sample , but it give me java.lang.IllegalArgumentException error on valueOf()



why the error prompt? is it because sql.Date only allow acccept yyyy-mm-dd pattern ? how i suppose to make sqlDate contain value of "utilDateStr" ?

thank you
Zip Ped
Ranch Hand

Joined: Jul 26, 2005
Posts: 336
I am not sure whether this is the shortest way of doing it, but it works:
public void showDate() {
java.util.Date utilDate = new java.util.Date();
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Calendar calendar = df.getCalendar();
java.sql.Date sqlDate = new java.sql.Date(calendar.YEAR,calendar.MONTH,calendar.DAY_OF_MONTH);
}
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18154
    
    8

Not only is Kailash's suggestion longer than Jeff's, it doesn't work properly. It passes constant values to the java.sql.Date constructor. A somewhat fixed version of it might look likePersonally I preferas Jeff already said.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
To continue what Paul, wrote... watch out for the fact that the java.util.Dates you get out of the database are normalized (zero h, m and s). The following example shows that two equal-looking java.util.Dates are not in fact equal:
I often write unit tests where I insert an object, select it and see the two beans are equal. This would get you into trouble in that scenario.
Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 437
thanks for all warm reply , i understand it now ....have a nice day to everyone ..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to parse java.util.Date to java.sql.date ?
 
Similar Threads
convert String to Date
Converting String to date "March 4, 2003"
Converting From String to java.sql.Date
SQl date conversion to java.util.Date object
convert a String into Date with SimpleDateFormat