cancel
Showing results for 
Search instead for 
Did you mean: 

how to convert " April 25,2005" to the format which it can be inserted

Former Member
0 Kudos

hi all,

how to convert " April 25,2005" date to the format which it can be inserted into database.

respond quickly

regards,

Joshua

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Joshua,

first of all, what exactly are you trying to do? It sounds like you have written some custom java component in which a user can enter a date value. You want to store this date value in a database.

Is this correct? If so, please give the code that is not working for you.

Next, some things you might consider worthful for your problem.

- Store your data value as a java.util.Date object (instead of String) internally. This makes it quite easy to use in an java.sql.PreparedStatement

- Identity the exact format your database requires data values (e.g. some DBs prefer #yyyy-mm-dd#).

Lastly, if you ask for "quick responses", please <b>DO </b> provide sufficient information for one to be able to acually answer you...

Regards,

Dominik

Former Member
0 Kudos

A bit more info required.

What format is required to be passed to the database? Is it ddmmyyyy, mmddyyyy, dd/mm/yyyy, etc?

From where will you be getting the value "April 25, 2005"? Are you expecting a user to type this into an input field or are you pulling it from a text file in that format or are you getting it like that from the Java date object, etc?

Respond quickly doesn't sound very polite in English.

Will you be giving feedback to anyone who responds with an answer this time (so that they or others with the same problem will know if the answer was usefull) or perhaps rewarding points for helpfull answers?

See

Former Member
0 Kudos

Thank you Patrick,

this is my problem,

GregorianCalendar gc = new GregorianCalendar();

gc.set(Calendar.YEAR, wcEvent.getYear());

gc.set(Calendar.WEEK_OF_YEAR, wcEvent.getWeek());

gc.add(Calendar.DAY_OF_MONTH,-1);

Date dt1=gc.getTime();

String dStr1 = DateFormat.getDateInstance().format(dt1);

after this i'm getting the date as a string Apr 25,2005

this i need to enter into database

but the problem is in schema i've declared the entry as "date" so it is throwing illegal argument exception

this is my preparedstatement

PreparedStatement pst = con.prepareStatement(str);

<b>pst.setDate(1, dStr1);</b>

pst.setString(2, Comments);

pst.setInt(3, '1');

pst.setString(4, user);

pst.setDate(5, new java.sql.Date(System.currentTimeMillis()));

pst.executeUpdate();

and by the way the previuos post actually i've sold the problem myself and i didnt notice the replies for this post ....dont mind

anyways i'll try to watch after solving also

regards,

Joshua

Former Member
0 Kudos

Hi Joshua,

I assume that you already tried changing the line;

pst.setDate(1, dStr1);

to

pst.setDate(1, dt1);

and it didn't work. Maybe because you have to pass a Java.sql.date and not a Java.util.date

How about changing the line;

String dStr1 = DateFormat.getDateInstance().format(dt1);

to

String dStr1 = DateFormat.getDateInstance(DateFormat.SHORT).format(dt1);

This will return the date in a completely numeric fashion, with day, month and year seperated by periods. You can then take these tokens and create a Java.sql.date object with them.

If I think of something better I'll post again.

Regards,

Patrick.

Former Member
0 Kudos

Joshua/Patrick,

I think I can provide the "something better"

Let's simply convert the util.Date object to a sql.Date object:


java.sql.Date sqlDate = new java.sql.Date(dt1.getTime());
pst.setDate(1, sqlDate);

This should do it.

Please post an answer if this solves your problem and don't forget to give points.

Regards,

Dominik

Former Member
0 Kudos

Precisely

I didn't check to see what java.util.Date.getTime() returned, it's a long so it can be passed straight to the java.sql.Date constructor. Nice one.