Reputation: 2019
I am using SimpleDateFormat
to convert a date string to java.sql.date.
On converting, I am getting a issue, i.e. Year is getting added automatically.
Code:
String d="2012-12-04T08:48:00";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd'T'HH:MM:SS");
java.util.Date date = null;
date = formatter.parse(d);
System.out.println("UTIL DATE: "+date);
System.out.println("SQL DATE: "+new Date(date.getTime()));
Output:
UTIL DATE: Fri Dec 04 08:12:00 IST 2015
SQL DATE: 2015-12-04
I should have got 2012 year in date, but it is adding up 3 years, Is there anything wrong in my code?
Thanks in Advance.
Upvotes: 2
Views: 137
Reputation: 159844
mm
represents minutes, MM
is used to match the months - swap the field patterns
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Upvotes: 2
Reputation: 425
Change your simple date format as of below code:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Here mm
is minutes and MM
for monts.
Upvotes: 1