?
Solved

how to get time component from jdbc resultSet

Posted on 2011-09-12
7
Medium Priority
?
377 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

718 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