Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

constructing a Date from a String

Status
Not open for further replies.

carpeliam

Programmer
Mar 17, 2000
990
US
To construct a java.util.Date from a String, I can use this:<br><br>Date myDate = (new SimpleDateFormat()).parse(myString);<br><br>To construct a java.sql.Date from a String, I can use this:<br><br>Here's my problem.<br><br>If myString is equal to something like this:<br>&quot;Tue Aug 01 00:00:00 EDT 2000&quot;<br>(this is the output of a particular java.util.Date) or if myString is equal to something like this:<br>&quot;2000-08-01 00:00:00.0&quot;<br>(that's the output of a particular java.sql.Date)<br><br>I'm getting a parsing exception. Obviously the date isn't formatted correctly; the thing is, I'm not sure what a string is supposed to look like so that in can be instantiated as a Date.<br><br>If somebody knows what a date should look like, please let me know... if somebody can test it out and make sure it works, that would be even better (please! my rear end is grass if this doesn't work flawlessly by Friday morning 8AM). Thanks so much. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence."
 
Try something like this for your second example:<br><br>SimpleDateFormat sdf= new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss.S&quot;);<br>Date yourDate = sdf.parse(&quot;2000-08-02 10:52:15.7&quot;);<br><br>Good luck<br>MAttias
 
My mistake- the type of object which will print out yyyy-MM-dd etc. is java.sql.Timestamp, which isn't giving me any problems at all. Neither is the SQL, which has a format of &quot;1969-12-31&quot;.<br><br>So it's just the &quot;Tue Aug 01 00:00:00 EDT 2000&quot; that's giving me a problem... how do I convert that into a java.util.Date? <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
Dear Liam,<br><br>java.util.Date dt = new java.util.Date();<br>String sdt = dt.toString();<br>System.out.println(sdt);<br>dt = new java.util.Date(java.util.Date.parse(sdt));<br>System.out.println(&quot;Parsed date : &quot; + dt.toString());<br><br><br>Hope this helps<br>-pete
 
Paul,<br><br>Date.parse() is a deprecated function... and if it wasn't, I'd just say dt = Date.parse() instead of instantiating a Date object with another Date object.<br><br>I was able to find my solution, though. I already knew I needed to use SimpleDateFormat, and I had something like this...<br><br>Date myDate = (new SimpleDateFormat()).parse(value);<br><br>but SimpleDateFormat can take multiple different date formats for parsing... so I needed to specify the format in the constructor, like this:<br><br>Date myDate = (new SimpleDateFormat(&quot;EEE MMM dd HH:mm:ss zzz yyyy&quot;)).parse(value);<br><br>thanks anyways... <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top