Default JDBC date format when reading date as a string from ResultSet

前端 未结 4 1450
鱼传尺愫
鱼传尺愫 2020-12-17 01:37

I\'m looking at some code that basically does the following:

ResultSet rs = ps.executeQuery();
String myDateStr = rs.getString(\"MY_DATE\"); //field is of ty         


        
相关标签:
4条回答
  • 2020-12-17 02:17

    For the Oracle JDBC driver I am using, the format is hard-coded in the driver library, so should only differ across systems if different driver versions are in use.

    See my attempt to get an answer to the same question here: Where is the date format specified when reading a date as a string from JDBC ResultSet.

    (Apologies for asking a separate question, but as your question had been answered multiple times with the ever-helpful "just don't do that" response, I tried again...).

    0 讨论(0)
  • 2020-12-17 02:25

    try this:

    ResultSet rs = ps.executeQuery();
    
    Date myDate = rs.getDate("MY_DATE"); 
    

    or this :

    ResultSet rs = ps.executeQuery();
    String myDateStr = rs.getString("MY_DATE"); 
    Date myDate = valueOf(myDateStr);
    

    More about date: http://docs.oracle.com/javase/7/docs/api/java/sql/Date.html
    More about ResultSet : http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html

    0 讨论(0)
  • 2020-12-17 02:37

    Instead of using

    String myDateStr = rs.getString("MY_DATE") 
    

    you should use

    Timestamp timestamp = rs.getTimestamp("MY_DATE");
    

    JDBC / Database will handle the date transformation for you.

    0 讨论(0)
  • 2020-12-17 02:39

    If the field is of type Date, then read it as a java.sql.Date and do whatever conversion you need after that. Otherwise you're at the mercy of the database implementation.

    0 讨论(0)
提交回复
热议问题