• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Nakata kokuyo
Ranch Hand
Posts: 472
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nakata kokuyo
Ranch Hand
Posts: 472
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 20990
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 472
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for all warm reply , i understand it now ....have a nice day to everyone ..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic