Converting oracle.sql.timestamptz to java.sql.Timestamp

I'm trying to convert the systimestamp of the (Oracle) database to a java.sql.Timestamp

(Part of) the code I have for testing the process is:

try {
    conn = this.getConnection();
    Statement stmt = conn.createStatement();

    stmt.execute("SELECT SYSTIMESTAMP FROM DUAL");

    ResultSet rs = stmt.getResultSet();
    ResultSetMetaData rsm = rs.getMetaData();

    while(rs.next()) {
        TIMESTAMPTZ ts = (TIMESTAMPTZ) rs.getObject(1);
        System.out.println(ts);
        System.out.println(ts.getClass());
        System.out.println(ts.timestampValue());
    }
} catch (SQLException e) {
    e.printStackTrace();
    System.out.println(e);
}

The output I get is:

oracle.sql.TIMESTAMPTZ@1e97f9f
class oracle.sql.TIMESTAMPTZ

and then an exception:
java.sql.SQLException: Conversion to Timestamp failed
at oracle.sql.Datum.timestampValue(Datum.java:283)
at com.sumawest.test.model.UnitTest.testTimezone(UnitTest.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at oracle.jdevimpl.junit.runner.TestRunner.doRun(TestRunner.java:111)
at oracle.jdevimpl.junit.runner.TestRunner.start(TestRunner.java:91)
at oracle.jdevimpl.junit.runner.TestRunner.main(TestRunner.java:43)
java.sql.SQLException: Conversion to Timestamp failed

Does anyone have an idea how I can solve this?

Answers


You could use the to_char function to convert it to a string and then use the value.

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

If you decompile the code you can see that you are calling the timestampValue method of the parent Datum class.

This directly throws the exception (no other code in the method). You need to call the timestampValue method of TIMESTAMPTZ itself passing a Connection.


Need Your Help

Rss in a WebView - iOS

iphone ios xcode rss webview

So I was doing a WebView for iOS on Xcode, everything is superfine but when i try to open an RSS url with the code below it doesn't work

tolower() function problems

c unix tolower

this is my example code:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.