Java Program to format date in mm-dd-yyyy hh:mm:ss format

Java 8Object Oriented ProgrammingProgramming

Let us format date in mm-dd-yyyy hh:mm:ss format −

// displaying date in mm-dd-yyyy hh:mm:ss format
Format f = new SimpleDateFormat("mm-dd-yyyy hh:mm:ss");
String str = f.format(new Date());
System.out.println("Current Date in MM-dd-yyyy hh:mm:ss format = "+str);

Since we have used the Format and SimpleDateFormat class above, therefore import the following packages. With that, we have also used the Date −

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

The following is an example −

Example

 Live Demo

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
public class Demo {
   public static void main(String[] args) throws Exception {
      // displaying current date and time
      Calendar cal = Calendar.getInstance();
      SimpleDateFormat simpleformat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
      System.out.println("Today's date and time = "+simpleformat.format(cal.getTime()));
      // displaying date in mm-dd-yyyy hh:mm:ss format
      Format f = new SimpleDateFormat("mm-dd-yyyy hh:mm:ss");
      String str = f.format(new Date());
      System.out.println("Current Date in MM-dd-yyyy hh:mm:ss format = "+str);
      // displaying date
      f = new SimpleDateFormat("dd/MMMM/yyyy");
      String strDate = f.format(new Date());
      System.out.println("Current Date = "+strDate);
      // current time
      f = new SimpleDateFormat("HH.mm.ss Z");
      String strTime = f.format(new Date());
      System.out.println("Current Time = "+strTime);
   }
}

Output

Today's date and time = Mon, 26 Nov 2018 09:34:11 +0000
Current Date in MM-dd-yyyy hh:mm:ss format = 11-26-2018 09:34:11
Current Date = 26/November/2018
Current Time = 09.34.11 +0000
raja
Published on 26-Dec-2018 11:39:58
Advertisements