java数据库日期格式化
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java数据库日期格式化相关的知识,希望对你有一定的参考价值。
我的数据库日期类型是Date型的。。。。
现在我想获取当前日期为(yyyy-MM-dd)型存入数据库。。
我用new Date();获取的都是带时间的。
现在只想获得Date型的格式日期!
谢谢。。
如果想在显示的时候格式化可以用SimpleDateFormat类
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String s = sdf.format(date); 参考技术A 日期格式转换的方式有很多,建议可以参考下:
select CONVERT(varchar, getdate(), 120 )
2004-09-12 11:06:08
select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),\'-\',\'\'),\' \',\'\'),\':\',\'\')
20040912110608
select CONVERT(varchar(12) , getdate(), 111 )
2004/09/12
select CONVERT(varchar(12) , getdate(), 112 )
20040912
select CONVERT(varchar(12) , getdate(), 102 )
2004.09.12
select CONVERT(varchar(12) , getdate(), 101 )
09/12/2004
select CONVERT(varchar(12) , getdate(), 103 )
12/09/2004
select CONVERT(varchar(12) , getdate(), 104 )
12.09.2004
select CONVERT(varchar(12) , getdate(), 105 )
12-09-2004
select CONVERT(varchar(12) , getdate(), 106 )
12 09 2004
select CONVERT(varchar(12) , getdate(), 107 )
09 12, 2004
select CONVERT(varchar(12) , getdate(), 108 )
11:06:08
select CONVERT(varchar(12) , getdate(), 109 )
09 12 2004 1
select CONVERT(varchar(12) , getdate(), 110 )
09-12-2004
select CONVERT(varchar(12) , getdate(), 113 )
12 09 2004 1
select CONVERT(varchar(12) , getdate(), 114 )
11:06:08.177 参考技术B 用String的format函数
具体如下:
Date date = new Date();
String time = Sring.format("%tF",date); 参考技术C SELECT CONVERT(VARCHAR(12),GETDATE(),111) 参考技术D simpledateformat
java 日期格式 毫秒
参考URL:http://www.busfly.net/csdn/post/java_string_fomat_date_time_simpledateformat.html
关键代码:
- java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
JAVA中各种日期表示字母如下:
字母 日期或时间元素 表示 示例
G |
Era 标志符 | Text | AD |
y |
年 | Year | 1996 ; 96 |
M |
年中的月份 | Month | July ; Jul ; 07 |
w |
年中的周数 | Number | 27 |
W |
月份中的周数 | Number | 2 |
D |
年中的天数 | Number | 189 |
d |
月份中的天数 | Number | 10 |
F |
月份中的星期 | Number | 2 |
E |
星期中的天数 | Text | Tuesday ; Tue |
a |
Am/pm 标记 | Text | PM |
H |
一天中的小时数(0-23) | Number | 0 |
k |
一天中的小时数(1-24) | Number | 24 |
K |
am/pm 中的小时数(0-11) | Number | 0 |
h |
am/pm 中的小时数(1-12) | Number | 12 |
m |
小时中的分钟数 | Number | 30 |
s |
分钟中的秒数 | Number | 55 |
S |
毫秒数 | Number | 978 |
z |
时区 | General time zone | Pacific Standard Time ; PST ; GMT-08:00 |
Z |
时区 | RFC 822 time zone | -0800 |
以上是关于java数据库日期格式化的主要内容,如果未能解决你的问题,请参考以下文章