2

After looking through many of the threads in stackoverflow and cant seem to find the correct method im looking for. If anyone could shine a bit of light it would be great.

So I populated a JCombo Box with SQL Dates but in doing so I formatted them to using SimpleDateFormat which converted them into a string with the format dd/MM/yyyy.

When the program is running he can select a certain date to query the database and so here the problem

Ive managed to make a String with a format of yyyy/MM/dd using string.split saving it into an array and ordering them in another string

Is There A Way To Convert This String (2014/04/13) into an SQL Date so I can query my database OR am I going about it incorrect

Thank You,

3 Answers 3

4

Is There A Way To Convert This String (2014/04/13) into an SQL Date?

Sample code:

    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

    try {
        java.util.Date utilDate = format.parse("2014/04/13");
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        System.out.println(sqlDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }

output:

2014-04-13

use SimpleDateFormat to parse a string into java.util.Date.

use java.sql.Date(miliseconds) constructor.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks Braj works a treat I just slotted my formatted String in instead of "2014/04/13". It was really bugging me for the last 2 hours. Did not think of bring in java.util.date even though java.sql.date is a subclass. Cheers again
yes you can do it in same manner java.util.Date newUtilDate=new java.util.Date(sqlDate.getTime());
I logic behind is the milliseconds that is used in constructor of both the classes.
getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
Is there any date in your database that is older than January 1, 1970?
|
2

Use java.util.SimpleDateFormat

An example here:

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
    java.lang.Date langDate = sdf.parse("2014/04/13");
    java.sql.Date sqlDate = new java.sql.Date(langDate.getTime());

2 Comments

Sorry you have used mm in place of MM. That is wrong. I have already posted it. Please look into again.
FYI, the terribly-designed java.sql.Date class has been supplanted by java.time.LocalDate.
0

tl;dr

myPreparedStatement.setObject( 
    … , 
    LocalDate.parse( 
        "23/01/2018" , 
        DateTimeFormatter.ofPattern( "dd/MM/uuuu" )
    ) 
) ;

java.time

The modern approach uses the java.time classes that supplanted the terrible Date/Calendar/SimpleDateFormat classes.

The LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.

Define a formatting pattern to match your input strings.

String input = "23/01/2018" ; 
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate localDate = LocalDate.parse( input , f ) ;
String output = localDate.toString() ;  // Generate text in standard ISO 8601 format.

JDBC 4.2

As of JDBC 4.2, we can exchange java.time objects with the database. Use smart objects rather than dumb text for database interactions.

myPreparedStatement.setObject( … , localDate ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.