<?xml version="1.0" encoding="utf-8"?><!-- generator="wordpress/2.0.4" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: How to Format Dates for SQL in Java</title>
	<link>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/</link>
	<description>Java programming tips and advice, and other topics</description>
	<pubDate>Wed, 19 Nov 2008 12:01:45 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.4</generator>

	<item>
		<title>by: Eddy</title>
		<link>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-8981</link>
		<pubDate>Tue, 25 Jul 2006 09:05:34 +0000</pubDate>
		<guid>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-8981</guid>
					<description>The main objective of this post is to show how to convert a &lt;code&gt;java.util.Date&lt;/code&gt; to a &lt;code&gt;java.sql.Date&lt;/code&gt; and does not dwelve deeply into exception handling.

For my other posts on exception handling, see:

&lt;a href="http://coding.mu/archives/2003/08/28/exception-handling-best-practice/" rel="nofollow"&gt;Exception Handling Best Practice&lt;/a&gt;
&lt;a href="http://coding.mu/archives/2003/09/03/ejb-exception-handling/" rel="nofollow"&gt;EJB Exception Handling&lt;/a&gt;
&lt;a href="http://coding.mu/archives/2004/03/02/exception-handling-or-result-code/" rel="nofollow"&gt;Exception Handling or Result Code&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>The main objective of this post is to show how to convert a <code>java.util.Date</code> to a <code>java.sql.Date</code> and does not dwelve deeply into exception handling.</p>
<p>For my other posts on exception handling, see:</p>
<p><a href="http://coding.mu/archives/2003/08/28/exception-handling-best-practice/" rel="nofollow">Exception Handling Best Practice</a><br />
<a href="http://coding.mu/archives/2003/09/03/ejb-exception-handling/" rel="nofollow">EJB Exception Handling</a><br />
<a href="http://coding.mu/archives/2004/03/02/exception-handling-or-result-code/" rel="nofollow">Exception Handling or Result Code</a>
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: G</title>
		<link>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-8954</link>
		<pubDate>Mon, 24 Jul 2006 22:16:11 +0000</pubDate>
		<guid>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-8954</guid>
					<description>A very good example of how NOT to handle exceptions!</description>
		<content:encoded><![CDATA[<p>A very good example of how NOT to handle exceptions!
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Java and friends &#187; Parsing Dates from String Values</title>
		<link>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-8805</link>
		<pubDate>Sat, 22 Jul 2006 08:44:21 +0000</pubDate>
		<guid>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-8805</guid>
					<description>[...] Hopefully, this will clear the apparent difficulty that beginners face when dealing with date formatting in Java. [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] Hopefully, this will clear the apparent difficulty that beginners face when dealing with date formatting in Java. [&#8230;]
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Eddy</title>
		<link>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-4149</link>
		<pubDate>Tue, 30 May 2006 11:19:04 +0000</pubDate>
		<guid>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-4149</guid>
					<description>Aasif,

My example shows you exactly how to do that.

Rgds,
Eddy</description>
		<content:encoded><![CDATA[<p>Aasif,</p>
<p>My example shows you exactly how to do that.</p>
<p>Rgds,<br />
Eddy
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Aasif</title>
		<link>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-4041</link>
		<pubDate>Tue, 30 May 2006 06:55:26 +0000</pubDate>
		<guid>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-4041</guid>
					<description>Hi all.
i have a date in the form of a string .the format is 15-05-2005 5:55:55.
Can any one tell how i can convert it to a sql.Date.</description>
		<content:encoded><![CDATA[<p>Hi all.<br />
i have a date in the form of a string .the format is 15-05-2005 5:55:55.<br />
Can any one tell how i can convert it to a sql.Date.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Robert Caranica</title>
		<link>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-9</link>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-9</guid>
					<description>I would recommend using the setLenient(false) method on the SimpleDateFormat object:

 &lt;pre&gt;
 String dateString = "23/08/2003";
 SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
 dateFormat.setLenient(false);
 &lt;/pre&gt;

 This disables the use of heuristics when parsing the date, to avoid unpredictable results when the input does not exactly match the format.

 I would also recommend an extra precaution: the use of the java.text.ParsePosition.

&lt;pre&gt;
 String dateString = "23/08/2003";
 String parseFormat = "dd/MM/yyyy";
 SimpleDateFormat dateFormat = new SimpleDateFormat(parseFormat);
 dateFormat.setLenient(false);
 ParsePosition parsePosition = new ParsePosition(0);
 java.util.Date d;
 try {
    d = dateFormat.parse(dateString, parsePosition);
    // Now check the ParsePosition index
    if(parsePosition.getIndex() == inputFormat.length()){
       // Parsing successful 
    }
    else{
       // Error occurred
    }
    ...
 }catch(Exception e){
 ...
 }
 &lt;/pre&gt;

 ParsePosition keeps track of the current position during parsing. After parsing, you can check the parsePosition.getIndex() to be the same as the parseFormat.length(). This ensures a correct parsing of the specified date using the specified date format.

 As to to actual problem you're faced with, I don't know a simpler way to achieve this.</description>
		<content:encoded><![CDATA[<p>I would recommend using the setLenient(false) method on the SimpleDateFormat object:</p>
<pre>
 String dateString = "23/08/2003";
 SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
 dateFormat.setLenient(false);
 </pre>
<p> This disables the use of heuristics when parsing the date, to avoid unpredictable results when the input does not exactly match the format.</p>
<p> I would also recommend an extra precaution: the use of the java.text.ParsePosition.</p>
<pre>
 String dateString = "23/08/2003";
 String parseFormat = "dd/MM/yyyy";
 SimpleDateFormat dateFormat = new SimpleDateFormat(parseFormat);
 dateFormat.setLenient(false);
 ParsePosition parsePosition = new ParsePosition(0);
 java.util.Date d;
 try {
    d = dateFormat.parse(dateString, parsePosition);
    // Now check the ParsePosition index
    if(parsePosition.getIndex() == inputFormat.length()){
       // Parsing successful
    }
    else{
       // Error occurred
    }
    ...
 }catch(Exception e){
 ...
 }
 </pre>
<p> ParsePosition keeps track of the current position during parsing. After parsing, you can check the parsePosition.getIndex() to be the same as the parseFormat.length(). This ensures a correct parsing of the specified date using the specified date format.</p>
<p> As to to actual problem you&#8217;re faced with, I don&#8217;t know a simpler way to achieve this.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Eddy Young</title>
		<link>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-10</link>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-10</guid>
					<description>Thank you for this tip, Robert.</description>
		<content:encoded><![CDATA[<p>Thank you for this tip, Robert.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: kelvinkline</title>
		<link>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-11</link>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-11</guid>
					<description>I cant believe how fd up java dates are...
NONE of these work. ALL are documented
// java.sql.Date dow = new java.sql.Date(System.currentTimeMillis());
// java.sql.Date dow = new java.sql.Date(103,10,28);
// java.sql.Date dow = new java.sql.Date.valueOf("2003-01-01");
// java.sql.Date.valueOf("2003-01-01");

How can rational humans have made a simple task so fd up???</description>
		<content:encoded><![CDATA[<p>I cant believe how fd up java dates are&#8230;<br />
NONE of these work. ALL are documented<br />
// java.sql.Date dow = new java.sql.Date(System.currentTimeMillis());<br />
// java.sql.Date dow = new java.sql.Date(103,10,28);<br />
// java.sql.Date dow = new java.sql.Date.valueOf(&#8221;2003-01-01&#8243;);<br />
// java.sql.Date.valueOf(&#8221;2003-01-01&#8243;);</p>
<p>How can rational humans have made a simple task so fd up???
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: kelvinkline</title>
		<link>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-12</link>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-12</guid>
					<description>It May be that my use of JDBC-ODBC instead of pure JDBC means that lots of date stuff fails.
Eddy Young's code (above) is the only way I have managed to get a date into a sql JDBC-ODBC database.  Apologies for my intemperate invective, but it took me best part of a day to find EY's code.  29Oct03</description>
		<content:encoded><![CDATA[<p>It May be that my use of JDBC-ODBC instead of pure JDBC means that lots of date stuff fails.<br />
Eddy Young&#8217;s code (above) is the only way I have managed to get a date into a sql JDBC-ODBC database.  Apologies for my intemperate invective, but it took me best part of a day to find EY&#8217;s code.  29Oct03
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Eddy Young</title>
		<link>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-13</link>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid>http://coding.moris.org/archives/2003/08/23/formatting-dates-for-sql-in-java/#comment-13</guid>
					<description>Glad the code was of help to you. Feel free to ask any question.</description>
		<content:encoded><![CDATA[<p>Glad the code was of help to you. Feel free to ask any question.
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
