DB2 中关于日期函数,我想让'2012-05',转化为Date格式,不知道怎么转??求帮助 还有怎么比较日期大小??

1. '2012-05'怎么转化为日期格式??
2. 两个日期格式之间怎么比较大小??

可以采用TO_DATE函数将'2012-05'转化为日期格式
TO_DATE('2012-05', 'YYYY-MM')
例子:
db2 => create table TEST2(c1 timestamp)
DB20000I The SQL command completed successfully.
db2 => insert into test2 values(to_Date('2012-05', 'YYYY-MM'))
DB20000I The SQL command completed successfully.
db2 => select * from test2

C1
--------------------------
2012-05-01-00.00.00.000000

1 record(s) selected.

第二个问题:
比较日期格式直接用< 或者>符号就可以比较日期格式了。
如下:
db2 => select * from test2 where c1 > current date

C1
--------------------------

0 record(s) selected.

db2 => select * from test2 where c1 <= current date

C1
--------------------------
2012-05-01-00.00.00.000000

1 record(s) selected.
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-12-10
用SimpleDateFormat来转换
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Date date = sdf.parse("2012-05");
这样即可,
日期比较大小如下:

String myString = "2012-05-01";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd);
Date d = sdf.parse(myString);

boolean flag = d.before(nowdate);
if(flag)
System.out.print("早于今天")
else
System.out.print("晚于今天")
相似回答