You can do it the same way as a java.util.Date (since java.sql.Date is a sub-class of java.util.Date) with a SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat(
    "MM-dd-yyyy");
int year = 2014;
int month = 10;
int day = 31;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1); // <-- months start
                                    // at 0.
cal.set(Calendar.DAY_OF_MONTH, day);

java.sql.Date date = new java.sql.Date(cal.getTimeInMillis());
System.out.println(sdf.format(date));

Output is the expected

10-31-2014

Answer from Elliott Frisch on Stack Overflow
🌐
Coderanch
coderanch.com › t › 385771 › java › java-sql-Date-dd-mm
java.sql.Date to 'dd-mm-yyyy' format (Java in General forum at Coderanch)
Hi Paul Clapham, Muhammad Saifuddin Thanks a lot for the response. Regards, Sandeep. ... Use SimpleDateFormat to set the format as you like.. DateFormat df=new SimpleDateFormat("dd-MMM-yyyy"); Date date=new Date(); String s=df.format(date); System.out.println(s);
Top answer
1 of 3
4

now I want to change the format I want to store the date in the format dd-MMM-yyyy

You don't need an explicit conversion into the requested date format dd-MMM-yyyy.
Dates are not directly concerned with date formats. Your SQL Driver class will convert to proper database specific format before inserting into a date field of database table.

Using MySQL Driver:

// this statement will cause sql date as '2014-03-21'  
new java.sql.Date( new java.until.Date().getTime() );

In most of the databases the default format is YYYY-MM-DD.

Example (MySQL):

mysql> show variables like 'date_format';
+---------------+----------+
| Variable_name | Value    |
+---------------+----------+
| date_format   | %Y-%m-%d |
+---------------+----------+
1 row in set (0.00 sec)

mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2014-03-21 |
+------------+
1 row in set (0.05 sec)

Detailed Example:

public class SimpleDateFormat_Example {
    public static void main(String[] args) throws Exception {
        String dateInputPattern = "yyyy-MM-dd"; // numeric 2 digit month
        String dateTargetPattern = "yyyy-MMM-dd"; // For 3 char month name
        String dateString = "2014-03-20";

        patternTest( dateInputPattern, dateString, dateTargetPattern );

        System.out.println();

        // day of month first and then 2 digit month
        dateInputPattern = "yyyy-dd-MM";  
        dateString = "2014-21-03";
        dateTargetPattern = "yyyy-MMMM-dd, EEEE"; // for Full month name

        patternTest( dateInputPattern, dateString, dateTargetPattern );
    } // psvm( ... )

    public static void 
    patternTest( String dateInputPattern, 
                 String dateString, 
                 String dateTargetPattern ) throws Exception {
        java.text.SimpleDateFormat sdf = 
            new java.text.SimpleDateFormat( dateInputPattern );
        java.util.Date date = sdf.parse( dateString );

        System.out.println( "Date Pattern: " + dateInputPattern );
        System.out.println( "Date String : " + dateString );
        System.out.println( "Date Value  : " + date );
        sdf.applyPattern( dateTargetPattern );
        System.out.println( "Target Pattern: " + dateTargetPattern );
        System.out.println( "Pattern based Date Value: " + sdf.format(date) );

        java.sql.Date sqlDate = new java.sql.Date( date.getTime() );
        System.out.println( "But, SQL Date: " + sqlDate );
    } // patternTest( s, s, s )
} // end of class SimpleDateFormat_Example

If you run the above program you would be seeing following results.

Results:

Date Pattern: yyyy-MM-dd
Date String : 2014-03-20
Date Value  : Thu Mar 20 00:00:00 IST 2014
Target Pattern: yyyy-MMM-dd
Pattern Formatted Date Value: 2014-Mar-20
But, SQL Date: 2014-03-20

Date Pattern: yyyy-dd-MM
Date String : 2014-21-03
Date Value  : Fri Mar 21 00:00:00 IST 2014
Target Pattern: yyyy-MMMM-dd, EEEE
Pattern Formatted Date Value: 2014-March-21, Friday
But, SQL Date: 2014-03-21

Have a close look at SQL Date, it has the same pattern in all the outputs. The same is also matching with the result of select curdate() pattern, as shown in top of this answer.

Conclusion:

Though you set and apply specific pattern, in your scripting language, for a date type database field, it would only be stored in the default pattern defined for the database date types.

Hence, converting a pattern from yyyy-MM-dd to yyyy-MMM-dd and sending to database will not affect and change anything.

2 of 3
2

try this code

String PATTERN="yyyy-MM-dd";
SimpleDateFormat dateFormat=new SimpleDateFormat();
dateFormat.applyPattern(PATTERN);
String date1=dateFormat.format(Calendar.getInstance().getTime());
🌐
Stack Overflow
stackoverflow.com › questions › 47482769 › format-java-sql-date-to-dd-mmm-yy-for-setdate
Format java.sql.Date to dd-MMM-yy for setDate - Stack Overflow
November 25, 2017 - Both java.util.Date and java.sql.Date are outmoded, troublesome old legacy classes. Use only java.time classes instead. myResultSet.getObject( … , LocalDate.class ).format( DateTimeFormatter.ofPattern( "dd-MMM-uu" ) ) ... Possible duplicate of How do I format a java.sql.date into this format: “MM-dd-yyyy”?.
🌐
Experts Exchange
experts-exchange.com › questions › 23457892 › How-can-I-format-java-sql-Date.html
Solved: How can I format java.sql.Date ? | Experts Exchange
June 4, 2008 - DateFormat df = new SimpleDateFormat("MM/dd/yy · yy"); String formatted = df.format(date); http://www.objects.com.au/java/qa/2123120718.html ... Objects Thanks for you commnet the issue is not display , the issue is saving with format that must be database is getting this date format vua JSP and I did not underestand how come we can format sqlDate in JSP but the same code is not apply to Java and jdbc
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › sql › Date.html
Date (Java Platform SE 7 )
s - a String object representing a date in in the format "yyyy-[m]m-[d]d". The leading zero for mm and dd may also be omitted.
Find elsewhere
🌐
W3Schools
w3schools.com › java › java_date.asp
Java Date and Time
import java.time.LocalDateTime; ... " + myDateObj); DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); String formattedDate = myDateObj.format(myFormatObj); System.out.println("After ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › sql › Date.html
Date (Java Platform SE 8 )
3 days ago - s - a String object representing a date in in the format "yyyy-[m]m-[d]d". The leading zero for mm and dd may also be omitted.
🌐
Coderanch
coderanch.com › t › 554892 › databases › convert-date-MM-dd-yyyy
Best way to convert date from MM/dd/yyyy to yyyy-mm-dd ? (JDBC and Relational Databases forum at Coderanch)
October 6, 2011 - Now, i convert the String (date recieved from user) to (yyyy-mm-dd) format by using the following java code , send it to valueOf() method of java.sql.Date class and then storing into DB.
🌐
SAP Community
community.sap.com › t5 › technology-q-a › making-java-sql-date-dd-mm-yyyy-format › qaq-p › 6093745
Solved: making java.sql.Date dd.mm.yyyy format - SAP Community
September 7, 2009 - Solved: Hi, java.sql.Date format is m/d/yyyy. I must use dd.mm.yyyy format. How can it be done? Thanks.