DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datetime = formatter.format(new java.util.Date());
通過java向mysql中插入datetime類型的數據:
String sql = "INSERT INTO wp_posts ( post_date )VALUES(?)";
PreparedStatement pstmt = connection.prepareStatement(sql);
Timestamp time = new Timestamp(System.currentTimeMillis());
pstmt.setTimestamp(1, time);
類似的有:
pstmt.setDate(10, new java.sql.Date(System.currentTimeMillis())); // 只有日期
pstmt.setTime(11, new Time(System.currentTimeMillis())); // 只有時間
pstmt.setTimestamp(12, new Timestamp(System.currentTimeMillis())); // 日期和時間
今天寫代碼主要是查找The Java Developers Almanac 1.4這本書,真是太好用了,例子好全。
轉自 http://blog.sina.com.cn/s/blog_4953e9290100c2tz.html