From the html input field I take in the date as date/month/year. Lets say the date entered by the user is
28/06/2013. I want to store this date in my mysql dbms.
How do I do this ? I want to store the date for later retrieval.
For example if the date is 2008/4/5 then we can format it by using below code...
String source="2008/4/5";
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
java.sql.Date d= new java.sql.Date(format.parse(source).getTime());
You should not use any user input directly in SQL. Instead, you should do parametrized queries, and define a java.sql.Date parameter. Then, use the SimpleDateFormat Java class to turn the user input into a Date object, and fill the parameter with it.
Related
I am trying to use new SimpleDateFormat to parse a string in the format dd-MM. Basically, I want to create a date object out of the string and persist in the database.
When I checked the database entry I see that it appends 1970 to the year column. I believe it is the default value of the year provided when it is empty. Is there a way to prevent the year value. I do not want to store information about the year.
My code -
String dateOfBirth = "14-Feb";
dbObject.save(new SimpleDateFormat("dd-MMM").parse(dateOfBirth));
For the sake of simplicity, assume dbObject.save() the method expects a date object to be provided. I do not want to create a date of value - 14-Feb-1970, instead it should be just 14-Feb.
I would strongly suggest you use the java.time.MonthDay class to store your dates. If your database doesn't support storing that, you can just store it as a string, and parse it when you get it out of the database.
Here is how you would parse your date:
MonthDay md = MonthDay.parse("14-Feb", DateTimeFormatter.ofPattern("dd-MMM").withLocale(Locale.US));
You can then store the string returned by .toString into the database (it will be something like --02-14), and the next time you parse it, you don't need a date time formatter:
MonthDay md = MonthDay.parse("--02-14");
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.
I want to insert into a Mysql column (DATETIME column) a time & date . For that purpose i use
the DATETIME field .
But on the JAVA side , I need to have a Date object with the time & date .
Consider the code :
Calendar cal = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String str = dateFormat.format(cal.getTime());
System.out.println(str);
This code produces :
2014/03/19 01:11:35
How can I convert it to a Date object in the format yyyy/MM/dd HH:mm:ss , so it would be
possible to put it in the mysql column of DATETIME ?
Much appreciated
The getTime() method of Calendar returns a Date object.
For JDBC, you'll use a java.sql.Timestamp to preserve the time component. (I think a java.sql.Date loses the time component.)
For example:
preparedStatement.setTimestamp(new java.sql.Timestamp(cal.getTime()));
First point: Date objects have no "format" - they just represent an instant in time.
If you print them, you get the default toString() implementation for Date.
Second point: You don't need to format a Date to use it with JDBC.
You should use a prepared statement, then call setObject(), to set the parameter value - the JDBC driver will do the rest, including wrapping in quotes if that's required for the data type.
Final point: Never use Calendar unless you have to.
In this case, simply new Date() will do the job.
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.
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.