利用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
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
相关文章推荐
- 时间戳格式化转换为日期格式,date.Format('yyyy/MM/dd hh:mm:ss')
- angularjs毫秒转成化日期格式(date:'yyyy-MM-dd HH:mm:ss')
- SimpleDateFormat的yyyy-MM-dd HH:mm:ss跟yyyy-MM-dd hh:mm:ss的区别
- 时间转换的好用方法(好招1)//Date date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2007-09-21 11:00:10.0100");
- 获取任意格式的new date,如yyyy-mm-dd、yyyy-mm等等,只需要SimpleDateFormat的formate(Date)和parse(String)两个函数即可
- javascript内置对象Date以及(面试题--打印当前的日期,格式为yyyy-mm-dd hh:mm:ss
- 日期格式问题NLS_DATE_FORMAT = ''yyyy-mm-dd HH24:MI:SS''
- Java continue break 制作简单聊天室程序,屏蔽不文明语言,显示每句话聊天时间 for(;;) SimpleDateFormat("yyyy-MM-dd hh:mm:ss") equalsIgnoreCase
- 计算两个日期之间相差的天数(带带负数) 支持格式YYYY-mm-dd和YYYY-mm-dd HH:mm:ss
- 用 SimpleDateFormat("yyyy-MM-dd HH:mm:ss") 做文件名要注意的。:
- 写一个方法对日期格式化 new Date().format("yyyy-MM-dd HH:mm:ss")
- java中设置日期格式的方式一般用SimpleDateFormat("YY-MM-dd")就可以了,但为什么有的 SimpleDateFormat("YY-MM-dd",locale.getDefau
- Java 原始 date格式的日期如何手动转成yyyy-MM-ss HH:dd
- dojo中DateTextBox日期格式yyyy-MM-dd转化为数据库中yyyyMMdd
- new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")?
- js 中按格式yyyy-MM-dd hh:mm:ss 格式化date
- Kettle5.2 couldn't convert string [...] to a date using format [yyyy/MM/dd HH:mm:ss.SSS]
- js获取当前日期,并且转化为时间格式“yyyy-MM-dd HH:MM:SS”
- JS获取当前的日期时间 格式“yyyy-MM-dd HH:MM:SS” 可以根据需求修改
- js 获取当前日期时间 格式为 yyyy-mm-dd hh:MM:ss