James Hutchinson
James Hutchinson

Reputation: 881

Invalid year exception with Oracle Date

I am attempting to save a date to my oracle database.

    long startTime = System.nanoTime();
    java.sql.Date startTimeDisplay = new Date(startTime);

    PreparedStatement st;
    String sql = "INSERT INTO J0T_JOB_STATUS (JST_JOB_ID, JST_JOB_CONFIGURATION_ID, JST_STATUS_CD, JST_START_TM, JST_END_TM, JST_CURR_STEP, JST_STEP_DETAILS, JST_MSG_COLLECTION) "
            + "VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
    st.setDate(4, startTimeDisplay);

This works just fine in our JUnit test suite which uses HSQL instead of oracle, but when using oracle I get:

    [err] Exception in thread "Thread-15" 
    [err] java.lang.IllegalArgumentException: Invalid year value
    [err]   at oracle.sql.TIMESTAMP.getOracleYear(TIMESTAMP.java:772)
    [err]   at oracle.sql.DATE.toBytes(DATE.java:533)
    [err]   at oracle.sql.DATE.<init>(DATE.java:121)
    [err]   at oracle.jdbc.driver.OraclePreparedStatement.setDate(OraclePreparedStatement.java:8747)
    [err]   at oracle.jdbc.driver.OraclePreparedStatementWrapper.setDate(OraclePreparedStatementWrapper.java:177)
    [err]   at com.ibm.ws.rsadapter.jdbc.WSJdbcPreparedStatement.setDate(WSJdbcPreparedStatement.java:1333)
etc.

table definition:

desc J0T_JOB_STATUS
Name                     Null Type         
------------------------ ---- ------------ 
JST_JOB_ID                    VARCHAR2(50) 
JST_JOB_CONFIGURATION_ID      NUMBER       
JST_STATUS_CD                 VARCHAR2(30) 
JST_START_TM                  TIMESTAMP(6) 
JST_END_TM                    TIMESTAMP(6) 
JST_CURR_STEP                 NUMBER       
JST_STEP_DETAILS              CLOB         
JST_MSG_COLLECTION            CLOB         

Upvotes: 0

Views: 5353

Answers (1)

Georgi Mihaylov
Georgi Mihaylov

Reputation: 61

According to Java Docs about System.nanoTime() (I'm assuming Java7, since you didn't provide your java version) :

This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary origin time (perhaps in the future, so values may be negative)

Link: https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#nanoTime()

I suggest using System.currentTimeMillis() instead unless you want to end-up persisting a negative number.

Upvotes: 2

Related Questions