2

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.

Abhishek
  • 1,999
  • 5
  • 26
  • 52

2 Answers2

2

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");
Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

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.

Imran
  • 429
  • 9
  • 23