首先展示一下这几个常用的时间类之间的关系
一 java.util.Date转化成String
java.util.Date udate=Calendar.getInstance().getTime(); Format f = new SimpleDateFormat("yyyy-MM-dd hh24:mm:ss"); System.out.println(f.form
二 java.util.Date转化java.sql.Date
java.util.Date udate=Calendar.getInstance().getTime(); java.sql.Date sdate = new java.sql.Date(udate.getTime());
preparedStatement.setDate(1, sdate );
但是这种方式,会造成日期后面的时分秒丢失。插入数据库里显示的是例如 2011-02-16 00:00:00
可以换成:
java.util.Date udate=Calendar.getInstance().getTime(); java.sql.Date sdate=new java.sql.Date(udate.getTime()); java.sql.Timestamp timestamp=new java.sql.Timestamp(sdate.getTime());
preparedStatement.setTimestamp(1, timestamp);
读取
java.util.Date udate=new java.util.Date(resultSet.getTimestamp(1).getTime());
三 几种时间类的控制台输出效果
import java.text.SimpleDateFormat; import java.util.Calendar; public class getDate { public static void main(String[] args) { java.util.Date ud=Calendar.getInstance().getTime(); System.out.println(ud); java.util.Date utilDate=new java.util.Date(); System.out.println(utilDate); java.sql.Timestamp tsp=new java.sql.Timestamp(utilDate.getTime()); System.out.println(tsp); java.sql.Date sqlDate=new java.sql.Date(utilDate.getTime()); System.out.println(sqlDate); java.sql.Time sTime=new java.sql.Time(utilDate.getTime()); System.out.println(sTime); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss S"); System.out.println(sdf.format(utilDate)); } }
输出如下:
Wed Feb 16 14:34:06 CST 2011
Wed Feb 16 14:34:06 CST 2011
2011-02-16 14:34:06.859
2011-02-16
14:34:06
2011-02-16 14:34:06 859
四 向数据库插入时间数据的例子
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; public class Main { public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,myDate TIMESTAMP );"); String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); java.sql.Timestamp sqlDate = new java.sql.Timestamp(new java.util.Date().getTime()); pstmt.setTimestamp(2, sqlDate); pstmt.executeUpdate(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); } private static void outputResultSet(ResultSet rs) throws Exception { ResultSetMetaData rsMetaData = rs.getMetaData(); int numberOfColumns = rsMetaData.getColumnCount(); for (int i = 1; i < numberOfColumns + 1; i++) { String columnName = rsMetaData.getColumnName(i); System.out.print(columnName + " "); } System.out.println(); System.out.println("----------------------"); while (rs.next()) { for (int i = 1; i < numberOfColumns + 1; i++) { System.out.print(rs.getString(i) + " "); } System.out.println(); } } private static Connection getConnection() throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver"); String url="jdbc:oracle:thin:@192.168.130.131:1521:ICSS"; String user="lixiang"; String password="19870924"; return DriverManager.getConnection(url,user,password); } }
贴一个专门讲java<—>数据库时间类型的的网址
http://www.java2s.com/Tutorial/Java/0340__Database/WorkingwithDateTimeandTimestampinJDBC.htm