Java Date convert from one format to another format

Description

Java Date convert from one format to another format

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

public class Main {

  public static void main(String[] args) {
    String strDate = "12/12/20";

    try {/* ww  w.j a v  a2s. c om*/
      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);
    }
  }
}



PreviousNext

Related