Solved

java.sql.Date

Posted on 2004-10-05
10
2,724 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now