Solved

how to get time component from jdbc resultSet

Posted on 2011-09-12
7
367 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 50
javap bin 2 36
Groovy problem when using SOAPUI : DispatchException occurred 7 46
How can i use WITH CTE for checking exist value? 3 34
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

822 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