Convert date string from one format to another format using SimpleDateFormat - Java Date Time

Java examples for Date Time:SimpleDateFormat

Description

Convert date string from one format to another format using SimpleDateFormat

Demo Code


import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
 
public class Main {
 
  public static void main(String[] args) {
    String strDate = "12/12/18";
   //from  w w w  .ja v a  2  s.  c  o  m
    try
    {
      SimpleDateFormat sdfSource = new SimpleDateFormat("dd/MM/yy");
     
      Date date = sdfSource.parse(strDate);
     
      SimpleDateFormat sdfDestination = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
     
      strDate = sdfDestination.format(date);
     
      System.out.println("Date is converted from dd/MM/yy format to MM-dd-yyyy hh:mm:ss");
      System.out.println("Converted date is : " + strDate);
     
    }
    catch(ParseException pe)
    {
      System.out.println("Parse Exception : " + pe);
    }
  }
}

Result


Related Tutorials