服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

利用simpledateformat 和messageformat 向数据库中插入yyyy-mm-dd 和yyyy-mm-dd hh:mm:ss的日期格式

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

扫描关注微信公众号