Solved

java.sql.Date

Posted on 2004-10-05
10
2,735 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
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tomcat not starting 6 69
configure dependency in POM for new database 3 34
How to determine if a string is a valid SHA value 7 36
Setup GlassFish 4 22
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

828 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