Java get current date time

Steps to get current date time in java

  1. Creating the SimpleDateFormat object with the desire pattern.
  2. Create an object of Date class.
  3. Call the format() method of DateFormat class and pass the date object as an argument to the method.

Java get current date time example

package com.w3schools;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class GetDateTime {
	public static void main(String args[]){
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
		String date = sdf.format(new Date()); 
		System.out.println(date);
	}
}

Output:

09-04-2018