Posted on

print date in specific format in Java

print date in specific format - Java:

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

public class DateFormat {
   * This method print date in specific format.
   * @param date date to print.
   * @return String Obtains the date in specific format.
   public String printDate(Date date) {
       //Variable to store the date in specific format
       String dateFormat = "";
       //Format the date
       SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");  
       dateFormat = formatter.format(date); 
       //Return the date in specific format
       return dateFormat;

   * One Example
   public static void main(String args[]) throws IOException {

      DateFormat  dateFormat= new DateFormat ();
      String date = dateFormat.printDate(new Date());
      System.out.println("Today is :" + date); //Today is :23/04/2020
Enter fullscreen mode Exit fullscreen mode

Top comments (0)