Weedoze Weedoze - 9 months ago 40
Java Question

Oracle date has not time from java

My application is using an Oracle database and one of the table contains a date column. When doing my queries inside SQLDeveloper I need to add the following line to get the date + hours

alter session set nls_date_format = 'DD/MM/YYYY HH24:MI:SS';

If I do not use this, I only see the date like

When I am doing my queries from my JAVA code I don't have the above line thus every date is returned in the format


String query = "SELECT date from SCHEMA.table";
ResultSet rs = null;
try {
connection = ConnectionFactory.getConnection(Database.ORACLE);
preparedStatement = connection.prepareStatement(query);

rs = preparedStatement.executeQuery();
while (rs.next()) {
java.sql.Date dateDB = rs.getDate("date");
catch(SQLException e){

How can I get the date with the hours ? Should I add
alter session ...
to each query ?

Answer Source

Description part of java.sql.Date:

To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero

Probably the java.sql.Timestamp is the correct type which includes the time components:

    java.sql.Timestamp dateDB = rs.getTimestamp("date");