Solved

java.sql.Date

Posted on 2004-10-05
10
2,743 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 75 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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 75 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 100 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

615 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