1.处理数据库,有DATE Java.sql.Date 日期,
TIME Java.sql.Time 时戳,TIMESTAMP Java.sql.Timestamp 当日日期和时间,
对应的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()
根据java2的规范要求使用Java.sql.Timestamp,这样不会失去精度详见(http://blogger.org.cn/blog/more.asp?name=hongrui&id=7557)。对于oracle数据库比较例外,可以用oracle.sql.TIMESTAMP
这和他的版本也有关系。注意,SQLserver中timestamp 对应的是 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.字符串转化时间,注意不能判断时间输入是否正确,判断时间输入是否正确,请使用正则。
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的功能。只有程序或系统抛出了异常,try-catch才能捕获 ,得到Wed Feb 01 00:00:00 CST 2006
这样操作是错误的,Timestamp是java.util.Date,会得到java.lang.ClassCastException,你犯了向下转型的错误。
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();
}
应该使用
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