0

I want to convert a string to date before storing it and I used

SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
Date returnDate = format.parse(date);

When I ran this with sample date: the input string for date conversion is 2014-05-06 the parsed date is Mon Jan 06 00:05:00 IST 2014

now when I store the returnDate in MySql the value is 2014-01-06 00:05:00

Why is the date changed ? Want to know if I am missing something. I went through the posts related to date string conversion : How to convert a date from a Datepicker to Mysql DATETIME format using java?

Community
  • 1
  • 1
Bhuvaneshwari
  • 82
  • 2
  • 16
  • go through the [Answer](http://stackoverflow.com/a/23492885/2764279) – earthmover May 07 '14 at 04:53
  • possible duplicate of [Convert date from string "yyyyddmm hh:mm:ss" to String "DD/MM/YYYY" in JAVA](http://stackoverflow.com/questions/23492808/convert-date-from-string-yyyyddmm-hhmmss-to-string-dd-mm-yyyy-in-java) – Elliott Frisch May 07 '14 at 05:12

2 Answers2

6

In your DateFormat use MM for month instead of mm, that is for minutes

Reference: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

You can use like this :

Date mDate= new Date(System.currentTimeMillis());
        SimpleDateFormat mDateFormat= new SimpleDateFormat("dd MMM yyyy HH:mm a");
        String dateformat=mDateFormat.format(mDate);


the string ["dd MMM yyyy HH:mm a"]  can be changed according to need of formate.

Like in your case : "yyyy-mm-dd