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.
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:
Save dates as Date and retrieve them as string with the requested format
Convert dates to strings and save them as formatted strings (VARCHAR for example)
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
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.
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');
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.
Related
I wanted to store date value as 20-Nov-2001 with Date type in mysql instead of YYYY-MM-DD format. what should i do?
You don't. You convert it in your app and store it as '1999-11-20', and on the way out if you need to display it as 20-Nov-1999 and don't want to reformat it in your app, you use:
SELECT DATE_FORMAT(my_date_field, '%d-%b-%Y') FROM my_table;
How to convert timestamp 1470989229.000014 to date format mm-dd-yyyy in java?
I could do it for different timestamp formats but I could not parse this one.
The database used is MySQL so the timestamp probably would be in that format.
first question, what programming language did you used?
in case you're using PHP, you can try date("mm-dd-yyyy", (1470989229.000014)) but if you want the result like 08-12-2016 , you can use date("m-d-Y", (1470989229.000014))
In MySQL you can do this using FROM_UNIXTIME followed by DATE_FORMAT function.
SELECT DATE_FORMAT(FROM_UNIXTIME(1470989229.000014),'%m-%d-%Y')
And the output(mm-dd-yyyy) would look like below:
08-12-2016
Demonstrated Here
I have a timestamp in my database. I want to fetch it from database using JAVA and display it to the user. How do I declare timestamp in JAVA? I am using Date, but it is storing yyyy-mm-ddd format. I want yyyy-mm-dd hh:mm:ss format.
Date date = siteBean.getAlertTime();
I want the date variable to hold yyyy-mm-dd hh:mm:ss
How can I do that?
You should consider working with Calendar, as Date is deprecated. The JavaDocs explains nicely how to replace your code with Calendar-methods.
Consider fetching the timestamp from your ResultSet as a String. You can manipulate the String within Java with the help of SimpleDateFormat.
Is it possible to save date values in the database of this format dd/MM/yy in Grails? I know I can customized the format in the views but I need the values to be returned as json and also return the values of dates in json in that format. Any suggestion will be appreciated.
Try this on your presentation layer, don't save time in that format in database. use following code to format the time according to your need.
Date date = new Date( );
SimpleDateFormat simpleFormat = new SimpleDateFormat ("dd/MM/yy");
System.out.println("Date: " + simpleFormat .format(date));
But if you want to save the data in this format in databse, then remember it returns a string and you will have to save the date in String format in database. Which I wouldn't recommend because of many reason.
You should not store the date as a formatted string because you lose the ability to do many things with that field, such as sort it or compare it. Always use the database's native date format for storage. If you want to change the format there are many places to do it, including the presentation layer (as others have suggested) and the database query layer. Format the date in the query if you want to do minimal processing in Java/Javascript.
Agreed with others you should not save in the database in String format. To format a Date using Groovy you can use the String.format() method.
String.format('%tY-%<tm-%<td', new Date())
See the Groovy dates documentation for further examples.
I have a JSP page in which I am entering a date in a textbox and retrieving the value from the textbox and the value is stored in a string variable. I want to convert this value to date and that value will be stored in database. I use MySQL.
Use SimpleDateFormat.parse() to parse your date string into a Date object and store that or the getTime() of that in the database.
Here's an example of parsing the date:
String pattern = "MM/dd/yyyy";
SimpleDateFormat format = new SimpleDateFormat(pattern);
Date date = format.parse("03/24/2011");
See this answer about storing dates in MySQL.
You will need to convert your date String into a java.sql.Date object, like so
java.sql.Date date = java.sql.Date.valueOf("2011-01-12"); //where date is of pattern "yyyy-MM-dd"
Once date is created, use the PreparedStatement.setDate(int index, java.sql.Date date) to store the date in the database.
If - for now - you do not need the actual Date in your program you can just call setString on the appropriate field in the prepared statement to have the database parse the date.
I would always store a value that does not depend on a timezone in a database; Date.getTime() does nicely and can always be converted to something you can display or work with otherwise, and it avoids lots of troubles when it comes to daylight saving time.