Display today’s date in Java with SimpleDateFormat

Java 8Object Oriented ProgrammingProgramming

To work with SimpleDateFormat class in Java, import the following package.

import java.text.SimpleDateFormat;

To get the date, use the format() method as shown below. Firstly, set the date format −

Calendar cal = Calendar.getInstance();
SimpleDateFormat simpleformat = new SimpleDateFormat("dd/MM/yyyy");

Now, we will get the date −

simpleformat.format(cal.getTime())

The following is the complete example −

Example

 Live Demo

import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Demo {
   public static void main(String[] args) throws Exception {
      Calendar cal = Calendar.getInstance();
      SimpleDateFormat simpleformat = new SimpleDateFormat("dd/MM/yyyy");
      System.out.println("Today's date = "+simpleformat.format(cal.getTime()));
   }
}

Output

Today's date = 26/11/2018
raja
Published on 26-Dec-2018 11:54:02
Advertisements