Code Hungry Code Hungry - 7 months ago 7
SQL Question

Incorrect output after converting to SqlDate

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Test {

/**
* @param args
*/
public static void main(String[] args) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
String dateInString = "2016-04-23";

try {

Date date = formatter.parse(dateInString);
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
System.out.println("Date: -- "+date);
System.out.println("FormatterDate : -- "+formatter.format(date));
System.out.println("SQLDATE : -- "+sqlDate);
} catch (ParseException e) {
e.printStackTrace();
}

}

}


Output :

Date: -- Sat Jan 23 00:04:00 CST 2016
FormatterDate : -- 2016-04-23
SQLDATE : -- 2016-01-23


My out put should be 2016-04-23 not 2016-01-23

Answer

As per the javadocs

m   Minute in hour

You want

M   Month in year