Solved

java.sql.Date

Posted on 2004-10-05
10
2,733 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

810 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