Reputation: 103
I want to save the dates in my database in the dd/mm/yyyy format, because I want to import the table that contains them in a JTable (Java SE) and I want to display them in this format. Is it possible to directly save the date on my database in this format or I must do it in another way? My DB is write in SQL and I use MySQL.
Upvotes: 1
Views: 1921
Reputation: 176
Use DATE_FORMAT function to change the format of a date in MySQL.
SELECT DATE_FORMAT(CURDATE(),'%d/%m/%Y')
SELECT DATE_FORMAT(column_name,'%d/%m/%Y') # FROM tablename
Refer to documentation for more details.
Upvotes: 0
Reputation: 1368
You can't do it, or else you have to use VARCHAR or CHAR, but thats not recommended.
save the date in DATE datatype with format yyyy-mm-dd. don't mess with it.
When you fetch the records, use DATE_FORMAT function to convert it into your format. (if you use MySQL)
like
SELECT DATE_FORMAT(CURDATE(), '%d-%m-%Y');
in your case
SELECT DATE_FORMAT(< your_date_field >, '%d-%m-%Y');
Upvotes: 0
Reputation: 26926
Date are dates. It doesn't exists a format for dates.
What you can obtain is a string with a particular format from the date.
Note that the format probably is not dd/mm/yyyy
but dd/MM/yyyy
because mm
is for minutes, not for months.
So basically you have two possibilities:
To convert a Date to a String in MySql you can use the function DATE_FORMAT
If you like to convert them in java you can use a SimpleDateFormat
Upvotes: 3
Reputation: 571
There is no possibility to save the date in the specified format but yes you can set the type of that field as String (in MySql varchar) and you can save whatever you want.
Upvotes: 0