store date in database - java

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.

Related

How to create a new Date object without the year parameter in Java?

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");

How to convert date-time "yyyy/MM/dd HH:mm:ss" that's stored in a String to a Date object?

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.

How to declare a variable which holds Timestamp value from database?

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.

storing the date in dbms

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.

How to getDate with yyyy-MM-dd format

How can I get the current date of the system with this format yyyy-MM-dd
I want this
SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String strDate = formatDate.format(now);
return strDate;
but returning a Date instead of a String.
Any help is greatly appreciated.
UPDATE: If that's the case, can I parse my String to Date?
How can i get the current date of the system with this format yyyy-MM-dd but returns Date instead of String.
You can't. There's no such thing as "a Date with a format" any more than there's the concept of "an int with a format". A Date value is just a point in time, with no associated text format, calendar system or time zone.
Using new Date() will get you a Date object representing the current instant in time, and nothing else. How you use that is up to you - but if you return it from a method then there is no associated date (as the date will vary by time zone), no format etc - it's up to the calling code to use it appropriately.
You might want to consider using Joda Time which at least has a LocalDate type - although you still need to consider which time zone you want to use when you think about "the current date". (And there's still no formatting information associated with the value.)
EDIT: To answer your update, you can just use SimpleDateFormat to parse - but it's not clear where your string has come from to start with. This sounds like the opposite requirement from the rest of your question.
since you cant change Date format build your own CustomDate, it is just a representation of time.
on the method which recieves the date as a string
use another simpledateformatter
and convert the string into date by using
simpledateformatter.parse(strDate);
You can use this .!!
String formatDate = new SimpleDateFormat("yyyy-MM-dd").format( yourDate);

Categories

Resources