java中格式化当前日期并转成字符串:
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())); // 日期和时间 |