Convert date to mysql date format

Following example will help you to convert date in the 01/11/2013 format to mysql accepted date format.

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

public class TesC {
public static void main(String args[]){

String start_dt = "20-11-2013";
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); 
Date date=null;
try {
date = (Date)formatter.parse(start_dt);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd");
String finalString = newFormat.format(date);
System.out.println(finalString);
}
}
Output is 2013-11-20

Comments