Convert Date Format Example Java Program


Convert Date Format Example Program

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

public class ConvertDateFormat{
    public static void main(String[] args) {
        try {
            String datee = "21/08/2012";
			System.out.println("The input date is:"+datee);
            DateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");
            Date date = format1.parse(datee);
            DateFormat format2 = new SimpleDateFormat("MM-dd-yyyy");
            datee = format2.format(date);
            System.out.println("Converted date is : " + datee);
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

Sample Output

Output is:
The input date is:21/08/2012
Converted date is : 08-21-2012