Solved

how to get time component from jdbc resultSet

Posted on 2011-09-12
7
360 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
  • 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now