您的位置:首页 > 数据库

利用SimpleDateFormat 和MessageFormat 向数据库中插入yyyy-mm-dd 和yyyy-MM-dd HH:mm:ss的日期格式

2005-08-03 12:03 946 查看
SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd"); //格式化当前系统日期
qds.setDate("制单日期", Date.valueOf(dateFm.format(new java.util.Date())));
此种情况数据库的字段为Date型
------------------------------------------------------------------------------------------------------------
String 制单日期 = MessageFormat.format("{0,date,yyyy-MM-dd HH:mm:ss}", new Object[] { new java.sql.Date(System.currentTimeMillis()) });
qds.setTimestamp("制单日期", Timestamp.valueOf(制单日期)); // 不能强行转换
此种情况数据库的字段为Timestamp(6)型
---------------------------------------------------------------------------------------------------------
附:几个Date类的valueOf()方法
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐