Solved

how to get time component from jdbc resultSet

Posted on 2011-09-12
7
375 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
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!

 
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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

631 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