Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

java.sql.Date

Posted on 2004-10-05
10
Medium Priority
?
2,749 Views
Last Modified: 2012-06-21
I have a date whcih I retrieve from a database.

java.sql.Date date =  resultset.getDate("DATE_FIELD")

this field is stored as a data type Date in oracle, an example  12/03/2004 12:35

I wish to obtain this string value, I use the simpleDateFormat class as
sdf = SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

I use the format method which gives me 12/03/2004, I also need the time part.

How can I do this?
0
Comment
Question by:inzaghi
[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
10 Comments
 
LVL 4

Expert Comment

by:JK2429
ID: 12228449
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html

Look at getTime().

This returns what you are lookign for.
0
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 300 total points
ID: 12228601
This example will help u

`import java.text.DateFormat;
import java.util.Date;

public class DateFormatExample1 {

    public static void main(String[] args) {
        // Make a new Date object. It will be initialized to the current time.
        Date now = new Date();

        // See what toString() returns
        System.out.println(" 1. " + now.toString());

        // Next, try the default DateFormat
        System.out.println(" 2. " + DateFormat.getInstance().format(now));

        // And the default time and date-time DateFormats
        System.out.println(" 3. " + DateFormat.getTimeInstance().format(now));
        System.out.println(" 4. " +
            DateFormat.getDateTimeInstance().format(now));

        // Next, try the short, medium and long variants of the
        // default time format
        System.out.println(" 5. " +
            DateFormat.getTimeInstance(DateFormat.SHORT).format(now));
        System.out.println(" 6. " +
            DateFormat.getTimeInstance(DateFormat.MEDIUM).format(now));
        System.out.println(" 7. " +
            DateFormat.getTimeInstance(DateFormat.LONG).format(now));

        // For the default date-time format, the length of both the
        // date and time elements can be specified. Here are some examples:
        System.out.println(" 8. " + DateFormat.getDateTimeInstance(
            DateFormat.SHORT, DateFormat.SHORT).format(now));
        System.out.println(" 9. " + DateFormat.getDateTimeInstance(
            DateFormat.MEDIUM, DateFormat.SHORT).format(now));
        System.out.println("10. " + DateFormat.getDateTimeInstance(
            DateFormat.LONG, DateFormat.LONG).format(now));
    }
}

0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12228643
following are results for above formats

 1. Tue Oct 05 22:20:00 GMT+05:30 2004
 2. 10/5/04 10:20 PM
 3. 10:20:00 PM
 4. Oct 5, 2004 10:20:00 PM
 5. 10:20 PM
 6. 10:20:00 PM
 7. 10:20:00 PM GMT+05:30
 8. 10/5/04 10:20 PM
 9. Oct 5, 2004 10:20 PM
10. October 5, 2004 10:20:00 PM GMT+05:30
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 14

Expert Comment

by:sudhakar_koundinya
ID: 12228666
Option 3,5,6,7 will help u in your case
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12228679
u can pass SQL date instead of java.util.Date to above example

Regards
Sudhakar
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12228690
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 300 total points
ID: 12229598
Use

java.util.Date date =  new java.util.Date(resultset.getTimestamp("DATE_FIELD").getTime());

then your SimpleDateFormat should work OK
0
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 12232496
Date d = resultset.getTimestamp("DATE_FIELD");
SimpleDateFormat sdf = SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
System.out.println(sdf.format(d));

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12235711
:-) objects' answer if more direct (i was forgetting that Timestamp extends Date)
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12235770
Thanks,
:-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 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:
Suggested Courses

661 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