Java Data Type How to - Convert mm/dd/yyyy to yyyy-mm-dd


We would like to know how to convert mm/dd/yyyy to yyyy-mm-dd.


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//from  w ww .j  a v  a  2  s .  c  om
public class Main {

  private static SimpleDateFormat inSDF = new SimpleDateFormat("mm/dd/yyyy");
  private static SimpleDateFormat outSDF = new SimpleDateFormat("yyyy-mm-dd");

  public static String formatDate(String inDate) {
    String outDate = "";
    if (inDate != null) {
        try {
            Date date = inSDF.parse(inDate);
            outDate = outSDF.format(date);
        } catch (ParseException ex){ 
    return outDate;

  public static void main(String[] args) {


The code above generates the following result.