简体   繁体   English

Java ResultSet.getString()的Date字段显示00:00:00.0

[英]Java ResultSet.getString() for Date field displaying 00:00:00.0

I am using a generic method to loop through and convert a ResultSet to a String array. 我正在使用一种通用方法来遍历并将ResultSet转换为String数组。 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 ? 我想知道为什么数据库中的日期显示2015-01-09时Oracle db值的Date列打印为2015-01-09 00:00:00.0?

Here is the code, the col type in Oracle is Date 这是代码,Oracle中的col类型是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

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? 因此,这是问题的第1部分,问题的第2部分-对于这里的通用方法,我最好的选择是执行.replace 00:00:00.0以将其删除吗?

You shouldn't be using rs.getString() on a Date datatype. 您不应该在Date数据类型上使用rs.getString() You should be using rs.getDate() and then parse the date as you wish. 您应该使用rs.getDate() ,然后根据需要解析日期。

Example: 例:

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

Or you can directly get date as string directly from the database using the TO_CHAR function like this: 或者,您可以使用TO_CHAR函数直接从数据库直接获取日期作为字符串,如下所示:

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

And then get it as string: 然后将其作为字符串获取:

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

Hope this helps 希望这可以帮助

The Oracle DATE datatype contains both a date and a time. Oracle DATE数据类型同时包含日期和时间。 So this isn't weird -- it is Oracle. 所以这并不奇怪-是Oracle。

I would personally convert the date to a java.sql.Date to remove the trailing time. 我个人将日期转换为java.sql.Date以删除拖尾时间。 You can use the ResultSet.getDate() method for this. 您可以为此使用ResultSet.getDate()方法。

See the docs on this. 请参阅有关此文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java 日期字段的 ResultSet.getString() 跳过毫秒 - Java ResultSet.getString() for Date field skips millisecond 如何在Java中将Resultset.getString()与String进行比较? - How to compare Resultset.getString() to String in Java? 将时间设为00:00:00.0 - Getting Time as 00:00:00.0 java:如何将日期从“ 2014年5月1日星期四00:00:00西部”投射到“ 2014-01-05 00:00:00.0” - java : How can I cast Date from “Thu May 01 00:00:00 WEST 2014 ” to “2014-01-05 00:00:00.0” 不可解析的日期java.text.ParseException:不可解析的日期:“ 2015-10-08 05:00:00.0” - Unparsable Date java.text.ParseException: Unparseable date: “2015-10-08 05:00:00.0” 日期转换从12:00:00到00:00:00 java - Date conversion from 12:00:00 to 00:00:00 java 如何将“2012-03-04 00:00:00.0”转换为日期格式为“dd-mm-yyyy HH:mm:ss”使用Java - How to convert “2012-03-04 00:00:00.0” to Date with Format “dd-mm-yyyy HH:mm:ss” Using Java 如何使用日期类获取格式为“ YYYY-MM-DD 00:00:00.0”的数据? - How can i get data in format “YYYY-MM-DD 00:00:00.0” using class Date? 需要更改ResultSet.getString(Value) - 有条件 - Need to change ResultSet.getString(Value)-— conditionally 是什么导致Java程序不同地对待两个ResultSet.getString()方法? - What is causing Java program to treat the two ResultSet.getString() methods differently?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM