Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / March 2007

Tip: Looking for answers? Try searching our database.

how to convert JAVA string in "hh:mm dd/mm/yyyy" to sql datetime format?

Thread view: 
shivaraj - 19 Mar 2007 11:38 GMT
Hi,
In my JAVA code I have a string in "15:45 03/17/2007" this format.
Could any one let me know how can i insert/update it on to sql
database where the column is of type datetime ? If any one has a
sample code to do this, that will be great.
Regards,
Shivaraj
Ian Wilson - 19 Mar 2007 12:49 GMT
> Hi,
> In my JAVA code I have a string in "15:45 03/17/2007" this format.
> Could any one let me know how can i insert/update it on to sql
> database where the column is of type datetime ? If any one has a
> sample code to do this, that will be great.

Your subject line says "hh:mm dd/mm/yyyy" but the example you quote is
not a valid date in that format, there is no month 17.

I'd use String split to decompose the date and time and the use String
concatenation to reassemble the parts in the form expected by SQL.
Arne Vajhøj - 19 Mar 2007 12:53 GMT
> In my JAVA code I have a string in "15:45 03/17/2007" this format.
> Could any one let me know how can i insert/update it on to sql
> database where the column is of type datetime ? If any one has a
> sample code to do this, that will be great.

Use SimpleDateFormat parse to convert your string to a
java.util.Date and use a PreparedStatement for using
it in SQL (you need to wrap it in a java.sql.TimeStamp, but that
is trivial).

Arne
shivaraj - 20 Mar 2007 11:28 GMT
> > In my JAVA code I have a string in "15:45 03/17/2007" this format.
> > Could any one let me know how can i insert/update it on to sql
[quoted text clipped - 7 lines]
>
> Arne

Hi Ian/Arne,

Thanks for your comments.
   Ian,
       by mistakly I said it as ""hh:mm dd/mm/yyyy" but it is ""hh:mm
mm/dd/yyyy" format. I will try splitting that string into date and
time and try to use it as sql date format.
   Arne,
       To try with this approach, I am not getting any patterns in
"hh:mm mm/dd/yyyy" in SimpleDateFormat to convert it into java date
format. If that is the case again I have to split it as Ian said. Is
that so or am i missing something?

Regards,
Shivaraj
Bruce Lewis - 20 Mar 2007 15:22 GMT
>         To try with this approach, I am not getting any patterns in
> "hh:mm mm/dd/yyyy" in SimpleDateFormat to convert it into java date
> format. If that is the case again I have to split it as Ian said. Is
> that so or am i missing something?

Read the API documentation about it.  We humans understand the two
different ways you use "mm", but SimpleDateFormat has its own way of
differentiating minutes and months.

Signature

http://ourdoings.com/
Amazingly simple photo sharing

Manuel T - 23 Mar 2007 13:00 GMT
> Read the API documentation about it.  We humans understand the two
> different ways you use "mm", but SimpleDateFormat has its own way of
> differentiating minutes and months.

Absolutely. Just use "mm" for minutes and "MM" for months.

Bye, Emanuele
Signature

Linux engine 2.6.17-11-generic i686 GNU/Linux

David Harper - 20 Mar 2007 20:14 GMT
[SNIP]
>     Ian,
>         by mistakly I said it as ""hh:mm dd/mm/yyyy" but it is ""hh:mm
[quoted text clipped - 5 lines]
> format. If that is the case again I have to split it as Ian said. Is
> that so or am i missing something?

You should read the Javadoc for java.text.SimpleDateFormat more carefully.

Lowercase 'm' denotes minutes in an hour, whereas uppercase 'M' denotes
months in a year.  You cannot assume that you can use 'm' for both month
and minute.

You also need to beware of the distinction between 'h' and 'H' for the
hour.  Both are valid, but they will yield different results.

Attention to detail is vital.

David Harper
Cambridge, England


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.