sudipta.dey
sudipta.dey

Reputation: 178

saving a date as dd/MM/yyyy in SQL database using NetBeans

String d=(((JTextField)dobdatetext.getDateEditor().getUiComponent()).getText());
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date fromDate = formatter.parse(d);  
java.sql.Date sqlDate = new java.sql.Date(fromDate.getTime());

I am trying to save sqlDate but in database it stores as yyyy/MM/dd format. Please somebody correct my mistakes so that i can get my desired results...

Upvotes: 1

Views: 5285

Answers (5)

Francis
Francis

Reputation: 643

in fact the format is used yyyy-MM-dd, just use a SimpleDateFormat to format the date value to the desired format when retriving a date value......

SimpleDateFormat dateFormat= new SimpleDateFormat("dd/MM/yyyy");
String strDate = dateFormat.format(rs.getDate("column name"));

Upvotes: 1

user2354035
user2354035

Reputation:

you can use the to_date function :

insert into table_name(column_name)
                       values ( TO_DATE( d, 'yyyy/MM/dd' ) );

Upvotes: 1

Song Gao
Song Gao

Reputation: 666

If you're storing the dates in text, SimpleDateFormat has an applypattern method, so you can try

formatter.applypattern("yyyy/MM/dd");
sqlDate = formatter.format(/*date you want to format*/);

Upvotes: 0

splungebob
splungebob

Reputation: 5415

If the database field is of type date (or whatever the vendor supports) and not text, then the format in the db is irrelevant. Formatting is for output.

Upvotes: 2

Vikdor
Vikdor

Reputation: 24134

The format of the date displayed in the default format configured in your SQL Server instance and doesn't depend on what date format you used in your application to set it.

Also, the format doesn't matter if you are storing date in a date column as you can retrieve the date in any format of your choice. See http://www.sql-server-helper.com/tips/date-formats.aspx on how to retrieve a data column in different date time formats. HTH.

Upvotes: 0

Related Questions