在 oracle.sql.TIMESTAMPTZ 和 DbUnit 的标准 JDBC 类之间转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/223096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 17:23:59  来源:igfitidea点击:

Converting between oracle.sql.TIMESTAMPTZ and standard JDBC classes for DbUnit

sqloracledatetimejdbcdbunit

提问by Jacob

I'm running Oracle 10g and have columns with Type_Name

我正在运行 Oracle 10g 并且有带有 Type_Name 的列

TIMESTAMP(6) WITH TIME ZONE

When inflated into java classes they come out as

当膨胀到 java 类时,它们会作为

oracle.sql.TIMESTAMPTZ 

But DbUnit can't handle converting Oracle specific classes to Strings for writing to XML. I'm wondering if there's any easy way for me to convert (say, in my SELECT statement somehow) from these Oracle specific timestamps to something in java.sql.

但是 DbUnit 无法处理将 Oracle 特定类转换为字符串以写入 XML。我想知道是否有任何简单的方法可以将这些 Oracle 特定时间戳转换(例如,在我的 SELECT 语句中)转换为 java.sql 中的某些内容。

采纳答案by Jamie Love

I haven't had to deal with this problem exactly, but I presume that having it come through as a string from the SELECT query would be fine.

我不必完全处理这个问题,但我认为将它作为来自 SELECT 查询的字符串来处理会很好。

You could use the to_charfunction. To convert it to a string. e.g:

您可以使用to_char函数。将其转换为字符串。例如:

SQL> select to_char(systimestamp, 'YYYY-MM-DD HH24:MI:SS.FF TZD') as d from dual;

D
----------------------------------
2008-10-21 17:00:43.501591

This would then be seen by your program as a string. TZDincludes timezone information (of which there is none in this example)

这将被您的程序视为字符串。TZD包括时区信息(在这个例子中没有)

Later, this could then be parsed by Java using the SimpleDateFormatclass.

稍后,这可以由 Java 使用SimpleDateFormat类进行解析。

Alternatively, the oracle.sql.TIMESTAMPTZclass has a method called dateValuethat returns a java.sql.Dateclass.

或者,oracle.sql.TIMESTAMPTZ类有一个dateValue返回java.sql.Date类的调用方法。

回答by Frank Puechl

I would like to remark that using IYYY as format for the year might not be a good idea unless you really want to get the ISO year. You should use YYYY instead of IYYY.

我想说的是,除非您真的想获得 ISO 年份,否则使用 IYYY 作为年份格式可能不是一个好主意。您应该使用 YYYY 而不是 IYYYY。

Try to run your SQL for 31.12.2012 using

尝试使用 31.12.2012 运行 SQL

select to_char(timestamp'2012-12-31 00:00:00 +00:00', 'IYYY-MM-DD HH24:MI:SS.FF TZD') as d from dual;

returns "2013-12-31 00:00:00.000000000" which is not the year you would expect.

返回“2013-12-31 00:00:00.000000000”,这不是您期望的年份。