views:

71

answers:

2

I have a problem with display of dates from Oracle database in a Java webapplication using a JSF datatable. The displayed date differs from the date in the database.

Here are some examples:

in oracle: 24-APR-87 display in datatable: Apr 23, 1987  
in oracle: 01-JAN-10 display in datatable: Dec 31, 2009  
in oracle: 13-MAR-89 display in datatable: Mar 12, 1989  

Here is my Java source code:

public Result getTable() {  
    try {  
        Class.forName(dbDriver);  
        con = DriverManager.getConnection(url);  
        ps = con.createStatement();  
        String query = "SELECT * from " + getTableName();  
        rs = ps.executeQuery(query);  
        return(ResultSupport.toResult(rs));  
    } catch(Exception e) { 
        return(null);
    }  
}  

What is the cause and solution for my problem?

+1  A: 

Oracle stores dates using an internal format that bears no relationship to how it's formatted for display.

What you're seeing is two different date formats being used by different frontends.

To force a query to use the format of your choice, it is best to use TO_CHAR, e.g.:

SELECT TO_CHAR(mydatecolumn,'DD-MON-YYYY') FROM mytable;
Jeffrey Kemp
I want my code can be used for all tables . how do I re-construct my code above to continue to be used by all the tables, but also applying TO_CHAR code?
irwan
The code you have in your question appears to return a result set without converting the dates to strings - perhaps the conversion is being done by the caller?
Jeffrey Kemp
here is my jsf-config: <managed-bean> <managed-bean-name>dataAll</managed-bean-name> <managed-bean-class>polimatPackage.Connection.getData</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean> and here is my jsp code: <h:outputText id="dateBirth" value="#{dataAll.dateBirth}" /> can you help me to explain or make a solution for these code...
irwan
so all your tables have a `dateBirth` column? I'm not sure that your generic "getTable" function is the best way here, but I'm not familiar with JSF so will say no more...
Jeffrey Kemp
not all.. ok, thank Jeffrey.. nice to meet you
irwan
+1  A: 

Try using f:convertDateTime converter with the JSF output and set the correct timeZone attribute for it.
Example (edit):

<h:outputText value="#{bean.myDate}">
    <f:convertDateTime timeZone="America/Los_Angeles" type="date" pattern="MM/dd/yyyy" />
</h:outputText>
Adam
can you give me an example for that?
irwan
Yes, I've edited the answer.
Adam