Java Data Type How to - Convert date in String format to another format








Question

We would like to know how to convert date in String format to another format.

Answer

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
//  www.  j a v  a  2s .c  o m
public class Main {

  public static void main(String[] args) throws Exception {

    String laDate = "2014-06-15";
    String dateString = laDate.substring(8, 10) + "/" + laDate.substring(5, 7)
        + "/" + laDate.substring(0, 4);
    Date date = new SimpleDateFormat("dd/MM/yyyy").parse(dateString);
    String dateFormat = "dd-MMM-yyyy";
    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, new Locale("en_US"));
    String tDate = sdf.format(date);
    System.out.println(tDate);

  }

}

The code above generates the following result.