SimpleDateFormat with Time Example in java

This example shows you how to get the Java date object in String format of “dd-MM-yyyy hh:mm:ss” using SimpleDateFormat object.

import java.text.SimpleDateFormat;
import java.util.Calendar;
public class SimpleDateFormatWithTime {
    public static void main(String[] args) {
        // Get the simple date format object by passing the format in string
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
        // get the date in String format by passing current date
        // in format method of simple date format object
        Calendar originalDate = Calendar.getInstance();
        System.out.println("The original date is : " + originalDate.getTime());
        String dateString = format.format(originalDate.getTime());
        // Print the formated date.
        System.out.println("The date in format dd-MM-yyyy hh:mm:ss is : " + dateString);

The output of above example will be as:

The original date is : Sun May 08 06:29:16 GMT 2011
The date in format dd-MM-yyyy hh:mm:ss is : 08-05-2011 06:29:16