1.處理數(shù)據(jù)庫,有DATE Java.sql.Date 日期,
TIME Java.sql.Time 時戳,TIMESTAMP Java.sql.Timestamp 當(dāng)日日期和時間,
對應(yīng)的ResultSet的方法
DATE java.sql.Date java.sql.Date getDate()
TIME java.sql.Time java.sql.Time getTime()
TIMESTAMP java.sql.Timestamp java.sql.Timestamp getTimestamp()
根據(jù)java2的規(guī)范要求使用Java.sql.Timestamp,這樣不會失去精度詳見(http://blogger.org.cn/blog/more.asp?name=hongrui&id=7557)。對于oracle數(shù)據(jù)庫比較例外,可以用oracle.sql.TIMESTAMP
這和他的版本也有關(guān)系。注意,SQLserver中timestamp 對應(yīng)的是 DateTime類型。
使用spring的jdbc時,盡可能用Java.sql.Timestamp詳見(http://blogger.org.cn/blog/more.asp?name=hongrui&id=9521)。
下面給個例子
public static java.sql.Timestamp getDBSysdate(Connection conn)
throws Exception {
Statement stmt = null;
ResultSet rs = null;
Timestamp sysTime = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT SYSDATE FROM DUAL");
if (rs.next()) {
sysTime = rs.getTimestamp("SYSDATE");
}
} catch (Exception e) {
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
} catch (Exception e) {
}
}
return sysTime;
}
最好的辦法使用long存時間,使用Calendar 處理,就是表示日期時間不直觀。
2.字符串轉(zhuǎn)化時間,注意不能判斷時間輸入是否正確,判斷時間輸入是否正確,請使用正則。
try
{
String st="2005-13-32 12:00";
java.text.DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
Date starttime = df.parse(st);
System.out.println(starttime.toString());
}
catch(Exception ex)
{
}
不要搞混了try-catch的功能。只有程序或系統(tǒng)拋出了異常,try-catch才能捕獲 ,得到Wed Feb 01 00:00:00 CST 2006
這樣操作是錯誤的,Timestamp是java.util.Date,會得到j(luò)ava.lang.ClassCastException,你犯了向下轉(zhuǎn)型的錯誤。
try
{
String st="2005-13-32 12:00";
java.text.DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
Timestamp starttime =(Timestamp) df.parse(st);
System.out.println(starttime.toString());
}
catch(Exception ex)
{
ex.printStackTrace();
}
應(yīng)該使用
try {
String st = "2005-12-2 12:00:00";
Timestamp starttime = Timestamp.valueOf(st);
System.out.println(starttime.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
或
import java.sql.*;
import java.util.*;
public class CreateTimestamp {
public static void main(String [] args) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2000);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DATE, 1);
cal.set(Calendar.HOUR_OF_DAY, 11);
cal.set(Calendar.MINUTE, 45);
cal.set(Calendar.SECOND, 30);
cal.set(Calendar.MILLISECOND, 0);
long millis = cal.getTime().getTime();
System.out.println("milliseconds in millis = " + millis);
java.sql.Timestamp ts = new java.sql.Timestamp(millis);
System.out.println("Timestamp ts before setting nanos = " + ts);
ts.setNanos(500);
System.out.println("Timestamp ts with nanos set = " + ts);
}
}
下面給出javadoc的方法
1. java.sql.Date.valueOf(java.lang.String)
public static Date valueOf(String s)
Converts a string in JDBC date escape format to a Date value.
Parameters:
s - a String object representing a date in in the format "yyyy-mm-dd"
Returns:
a java.sql.Date object representing the given date
Throws:
IllegalArgumentException - if the date given is not in the JDBC date escape format (yyyy-mm-dd)
2. java.sql.Time.valueOf(java.lang.String)
public static Time valueOf(String s)
Converts a string in JDBC time escape format to a Time value.
Parameters:
s - time in format "hh:mm:ss"
Returns:
a corresponding Time object
3. java.sql.Timestamp.valueOf(java.lang.String)
public static Timestamp valueOf(String s)
Converts a String object in JDBC timestamp escape format to a Timestamp value.
Parameters:
s - timestamp in format yyyy-mm-dd hh:mm:ss.fffffffff
Returns:
corresponding Timestamp value
Throws:
IllegalArgumentException - if the given argument does not have the format yyyy-mm-dd hh:mm:ss.fffffffff