Java Data Type How to - Format String date value to another string format








Question

We would like to know how to format String date value to another string format.

Answer

import java.text.SimpleDateFormat;
import java.util.Date;
/*w w w.j  a  va2s .c o m*/
public class Main
{
    public static void main(String[] args) throws Exception
    {
        String source= new String("03/20/2015");
        SimpleDateFormat sdfinput = new SimpleDateFormat("MM/dd/yyyy");
        SimpleDateFormat sdfoutput = new SimpleDateFormat("yyyy-MM-dd");
        Date inputdate=sdfinput.parse(source);
            String outputDate=sdfoutput.format(inputdate);
        System.out.println(outputDate);
    }
}

The code above generates the following result.