Solved

how to get time component from jdbc resultSet

Posted on 2011-09-12
7
371 Views
Last Modified: 2012-05-12
I have a datetime component in an SQL Server 2005 table column. When I do:

Date myDate = resultSet.getDate("myDate");

I only get the date component, not the time component. How do I get the time?
0
Comment
Question by:jmarkfoley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36523006


you normally do it like that


Date dd = null;
while(resultSet.next()){
 dd = resultSet.getDtae("date_fuiled_name");

}

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

System.out.println(sdf.format(dd));


 
jva.sqlDate which is returned by getDate() method normmal has ball components
- both date and time if you have time stored in datbase,
it is only if you just print it it has toString() metghod which
prints only date.
If you format it correctly =- it should print both data and time

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36523035
its is just that System.out.println(java.sql.Dtae) prints only date
but the actula java.sqlDate obnject would contain both date and time

anothe option is to format date by the means of SQL server
in the select statement and then use resultSet.getString(column number) method rather than getDate() method
Oracle has to_char(Date)  function to format date, I';m sure SQL server has something similar
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36523162


see for example here how to format date in select statemnt in SQL server
http://www.sql-server-helper.com/tips/date-formats.aspx

after you fromat date in select starement you can use resultSet.getString()
method of resultSet to get the date wghich will inclode the time element also
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 1

Author Comment

by:jmarkfoley
ID: 36523351
for_yan: If any of you have actually done this I'd be interested in seeing the code. I am formatting using the time component. I'm using the same format string as you specified:

Date maxrun = rs.getDate("maxRun");
lastCheckRun = (new SimpleDateFormat("MM/dd/yy HH:mm:ss")).format(maxrun,new StringBuffer(),new  FieldPosition(0)).toString();
out.println("<br>" + lastCheckRun);

This produces "09/11/11 00:00:00" whereas the actual datetime value is "09/11/11 06:33:08.0"

Yes, I could format it using SQL Server, but I do want an actual java Date object in my program because I want to do manipulations and calculations. If there is no way to get the time component otherwise, I suppose I could use the SQL Server formatting and then simpleDateFormat.parse() to get the Date, but that seems a bit dramatic. Surely there is a way to get the date component!
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 500 total points
ID: 36523414
Try this,

Timestamp ts = rs.getTimestamp( "myDate" ) ;
Date myDate = new Date( ts.getTime() ) ;
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36523454
I did this with Oracle, not with SQL server
and if I need time I indeed prefer to format it with the database

0
 
LVL 1

Author Closing Comment

by:jmarkfoley
ID: 36523800
Thanks ksivananth! That did the trick.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
glassfish admin console 1 51
jmeter usage 4 63
Are triggers slow? 7 29
What Is an Error? 2 59
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question