Java ResultSet.getString()的Date字段显示00:00:00.0 [英] Java ResultSet.getString() for Date field displaying 00:00:00.0

查看:312
本文介绍了Java ResultSet.getString()的Date字段显示00:00:00.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一种通用方法来遍历并将ResultSet转换为String数组.我想知道为什么数据库中的日期显示2015-01-09时,Oracle db值的Date列打印为2015-01-09 00:00:00.0?

I am using a generic method to loop through and convert a ResultSet to a String array. I am wondering why does the Date column in Oracle db value come print out as 2015-01-09 00:00:00.0 while the date in the database shows 2015-01-09 ?

这是代码,Oracle中的col类型是Date

Here is the code, the col type in Oracle is Date

    while(rs.next()) {
        String[] arr = new String[colCount];
        for(int i = 1; i <= colCount; i++) {
            arr[i-1] = rs.getString(i);
        }//end for
        list.add(arr);
    }//end while

所以这是问题的第1部分,问题的第2部分-对于这里的通用方法,我最好的选择是执行.replace 00:00:00.0来删除它?

So that is part 1 of the question, part 2 of the question - is my best option for a generic method here to do a .replace 00:00:00.0 to remove that?

推荐答案

您不应在Date数据类型上使用rs.getString().您应该使用rs.getDate(),然后根据需要解析日期.

You shouldn't be using rs.getString() on a Date datatype. You should be using rs.getDate() and then parse the date as you wish.

示例:

java.sql.Date date = rs.getDate(i);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = dateFormat.format(date);

或者您可以使用TO_CHAR这样的函数直接从数据库中直接获取日期作为字符串:

Or you can directly get date as string directly from the database using the TO_CHAR function like this:

SELECT TO_CHAR(col1, 'yyyy-mm-dd') AS 'MYDATE' FROM TABLE1;

然后将其作为字符串获取:

And then get it as string:

String dateStr = rs.getString("MYDATE");

希望这会有所帮助

这篇关于Java ResultSet.getString()的Date字段显示00:00:00.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆