在java中怎么从year,month,day得到java.sql.Date?(50分)

  • 主题发起人 主题发起人 曹晓钢
  • 开始时间 开始时间

曹晓钢

Unregistered / Unconfirmed
GUEST, unregistred user!
我用
new java.sql.Date(Year,Month,Day);
编译的时候有个警告:
The constructor invoked to create java.sql.Date with arguments (int, int, int) is deprecated
帮助说是应该有同名的方法。
那么现在正确的写法应该是怎么写的?
 
Deprecated. As of JDK version 1.1, replaced by
Calendar.set(year + 1900, month, date)
or GregorianCalendar(year + 1900, month, date).
 
I readdo
cument, since 1.1, the whole java.util.Date has been deprecated.
java.sql.Date comes from java.util.Date, so did it.
then
how to use Calendar to access JDBC?
my current code is like
java.sql.Date d = xxxx;
q.SQL = " select **** from *** when DATEXXX = '"+d.toString()+"'";
Do I have to create two class: Calendar, DateFormat todo
the same thing?
and I didn't find how to use java.text.DateFormat to generate a String that
JDBC can use .
Would you please give me a way?
Thanks!
 
看看下面的能不能行。
用DateFormat的好处主要是他根据你的Locale而变化。
==============================================================================
Calendar clndr = Calendar.getInstance();
clndr.set(2000,10,26);
System.out.println(DateFormat.getDateInstance(DateFormat.SHORT).format(clndr.getTime()));
=============================================================================
如果不行的话,试一试下面两个:
=============================================================================
System.out.println(
DateFormat.getDateInstance(DateFormat.LONG,Locale.PRC).format(clndr.getTime()));
System.out.println(
new SimpleDateFormat("yyyy-MM-dd").format(clndr.getTime()));
 
Data的 new Data(int ,int,int)虽然被禁了,可以用Calendar来转换一下。
Calendar cldNow = Calendar.getInstance();
cldNow.set(year,month,day);
Data datNow = new Data(cldNow.getTimeInMillis());
应该是好使的吧!
 
protected long getTimeInMillis();
Gets this Calendar's current time as a long.
 
曹晓钢:
If you want not to make your current code change much,just use follow code:
java.sql.Date d =java.sql.Date.valueOf("2000-10-28");
q.SQL = " select **** from *** where DATEXXX = '"+d.toString()+"'";
 
嘿嘿,各位看来都明白。:)
就50分,还是分给JJams 和uuu好了。
 
多人接受答案了。
 
哈哈,曹大虾分分错了,应该给我和eguy的。因为:
大家试过YoYoYo的方法吗????
下面可是从文档里面贴出来的,仔细看
============================================================================
<table>
<tr>
<td bgColor="#cce6ff" class="text" width="40%"><strong>来自:</strong><a href="http://www.delphibbs.com/delphibbs/Dispuser.asp?UserName=JJams_King">JJams_King</a></td>
<td bgColor="#cce6ff" class="text" width="40%"><strong>时间:</strong>00-10-27
18:42:41</td>
<td bgColor="#cce6ff" class="text" width="20%">ID:378524</td>
</tr>
<tr>
<td colSpan="3" width="100%">
<table border="0" cellPadding="10" width="100%">
<tbody>
<tr>
<td bgColor="#f7f7f7" width="100%">
<pre class="text"><font color="#FF0000">protected</font><font color="#0000a0"> long getTimeInMillis();
Gets this Calendar's current time as a long. </font></pre>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
 
〉来自:eguy 时间:00-10-28 10:04:02 ID:379008
〉曹晓钢:
〉If you want not to make your current code change much,just use follow code:
〉java.sql.Date d =java.sql.Date.valueOf("2000-10-28");
〉q.SQL = " select **** from *** where DATEXXX = '"+d.toString()+"'";
哈哈,又发现好玩的。
String --> Date --> String
循环了,哈哈。
 
后退
顶部