I know it’s a common question but I couldn’t find how to store a java.util.Date object in MySQL.
Is it possible? if not what kind of solution you reccomand me since I need this accurate time stored in my MySQL database.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Have a look at the Calendar class.
Simply call
Calendar.getInstance()to grab yourself an instance, then you can useget(Calendar.YEAR);to get the year,get(Calendar.DAY);to get the day, and so on.You can set the time in a similar way by using the
set()method.If you want to convert this to a date object, use the
getTime();method, and if you want to format the date into a string then have a look atSimpleDateFormat.As a general example:
You can also use a
SimpleDateFormatobject to parse the date like so:If you’re using the
SimpleDateFormatmore than once, then best to have it as a separate variable rather than keep recreating it.