java createSQLQuery().list()返回日期格式没有时分秒的解决方法(转)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java createSQLQuery().list()返回日期格式没有时分秒的解决方法(转)相关的知识,希望对你有一定的参考价值。
java createSQLQuery().list()返回日期格式没有时分秒的解决方法(转)
J2EE项目中用使用Date类型不显示时分秒的解决方法
2010-04-21 21:35:00| 分类: java技术 |字号 订阅
昨天,阿堂(网络时空)在开发web项目中,发现提取Oracle中某几个Date类型的字段时,发现提出来的Date型值,只有“年月日”,没有“时分秒”显示出来,实际业务中,是要求页面上显示的日期要求是有“年月日是分秒”的。这里说明一下,我是用session.createSQLQuery(sql).list()来返回值的,反回的值类型是List(Object[]).虽然,我是用的ssh,由于此处的sql对应的业务情况涉及到复杂的多表查询,如果是用hibernate来作映射的话,难度就非常大了,而且编码会带来很大的难度。。所以这里就委而用原生态的sql或者说标准sql来返回值了。
具体详细问题如下
在用ssh中
对于如下语句
session.createSQLQuery(sql).list();
查询,返回一个List<Object[]>
在对List循环中
我发现 如 显示日期类型的值时,只显示了“年月日”,没有显示“时分秒”
for(Object[] obj:session.createSQLQuery(sql).list())
{
...
System.out.println("收单日期是 "+(Date)obj[5]);
//只显示了年月日,没有显示"时分秒"了 如2010-03-01
//oracle数据表中对应字段的值,实际上是03-01-2010 16:31:14
...
}
折腾了半天后,经网友提醒,和本人的耐心测试,终于找到了解决方法
方法一
将Oracel数据库对应表中“收单时间的字段”receive_sheet_time,由原来的Date类型改为timestamp
然后,在java程序中,由 (java.util.timestamp)obj[5] 这样返回值,就会显示有“年月月时分秒“了。
如 2010-03-01 14:39:17
方法二
OracleOracel数据库对应表中“收单时间的字段”receive_sheet_time,字段继续保持Date类型不变
将上述sql语句中,返回 “收单日期的字段”receive_sheet_time ,变为
to_char(loading.receive_sheet_time,‘YYYY-MM-DD hh24:mi:ss‘) as receive_sheet_time
作为字符串类型来返回值,
然后在java程序中用,由 (String)obj[5] 这样返回值,就会显示有“年月月时分秒“了。
如 2010-03-01 14:39:17
附:至于Date和timestamp类型的区别,我就不多说,朋友们可以到网上一搜就知道了
原来的sql
SELECT load_point,
dispatch_no,
schedule_no,
truck_no,
trans_date,
trans_company_name,
departure_time,
trans_company_code,
receive_sheet_time,
dispatch_sheet_time,
loading_time,
SUM(expected_qty) AS expected_qty,
SUM(allocate_qty) AS allocate_qty,
SUM(real_qty) AS real_qty
FROM (SELECT odetail.load_point,
dispatch.dispatch_no,
odetail.Schedule_No,
dispatch.trans_company_code,
dispatch.truck_no,
TO_CHAR(dispatch.trans_date, ‘YYYY-MM-DD‘) AS trans_date,
dispatch.trans_company_name,
odetail.expected_qty,
odetail.allocate_qty,
odetail.real_qty,
loading.receive_sheet_time,
loading.dispatch_sheet_time,
loading.loading_time,
loading.departure_time
FROM t_dispatch dispatch,
t_order_detail odetail,
t_dispatch_detail tdetail,
t_dispatch_loading loading
WHERE dispatch.dispatch_no = tdetail.dispatch_no
AND odetail.order_no = tdetail.order_no
AND odetail.order_line_no = tdetail.order_line_no
AND loading.dispatch_no = dispatch.dispatch_no
AND loading.load_point = odetail.load_point
AND dispatch.status <> 99
AND NVL(dispatch.b_cancel, 0) = 0)
WHERE 1 = 1
AND TO_CHAR(departure_time, ‘YYYY-MM-DD HH24:MI:SS‘) >=
‘2010-04-07 00:00:07‘
AND TO_CHAR(departure_time, ‘YYYY-MM-DD HH24:MI:SS‘) <=
‘2010-04-07 23:59:07‘
GROUP BY load_point,
dispatch_no,
schedule_no,
truck_no,
trans_date,
trans_company_name,
departure_time,
trans_company_code,
receive_sheet_time,
dispatch_sheet_time,
loading_time
ORDER BY dispatch_no, schedule_no, load_point
新sql语句
(下述sql语句红色部分,就是我将返回的日期类型,转换为to_char类型返回了。。用方法一时,用原来的sql就行了)
SELECT load_point,
dispatch_no,
schedule_no,
truck_no,
trans_date,
trans_company_name,
departure_time,
trans_company_code,
receive_sheet_time,
dispatch_sheet_time,
loading_time,
SUM(expected_qty) AS expected_qty,
SUM(allocate_qty) AS allocate_qty,
SUM(real_qty) AS real_qty
FROM (SELECT odetail.load_point,
dispatch.dispatch_no,
odetail.Schedule_No,
dispatch.trans_company_code,
dispatch.truck_no,
TO_CHAR(dispatch.trans_date, ‘YYYY-MM-DD‘) AS trans_date,
dispatch.trans_company_name,
odetail.expected_qty,
odetail.allocate_qty,
odetail.real_qty,
to_char(loading.receive_sheet_time,‘YYYY-MM-DD hh24:mi:ss‘) as receive_sheet_time,
to_char(loading.dispatch_sheet_time,‘YYYY-MM-DD hh24:mi:ss‘) as dispatch_sheet_time,
to_char(loading.loading_time,‘YYYY-MM-DD hh24:mi:ss‘) as loading_time,
to_char(loading.departure_time,‘YYYY-MM-DD hh24:mi:ss‘) as departure_time
FROM @DBUSer.t_dispatch dispatch,
@DBUSer.t_order_detail odetail,
@DBUSer.t_dispatch_detail tdetail,
@DBUSer.t_dispatch_loading loading
WHERE dispatch.dispatch_no = tdetail.dispatch_no
AND odetail.order_no = tdetail.order_no
AND odetail.order_line_no = tdetail.order_line_no
AND loading.dispatch_no = dispatch.dispatch_no
AND loading.load_point = odetail.load_point
AND dispatch.status <> 99
AND NVL(dispatch.b_cancel, 0) = 0)
WHERE 1 = 1
@var1
GROUP BY load_point,
dispatch_no,
schedule_no,
truck_no,
trans_date,
trans_company_name,
departure_time,
trans_company_code,
receive_sheet_time,
dispatch_sheet_time,
loading_time
ORDER BY dispatch_no, schedule_no, load_point
以上是关于java createSQLQuery().list()返回日期格式没有时分秒的解决方法(转)的主要内容,如果未能解决你的问题,请参考以下文章
hibernate:java.math.BigInteger cannot be cast to java.lang.Long
CreateSQLQuery(string) 抛出 System.ArgumentException