Get current date, time , timestamp in Java

You can get the current date time via following two classes – Date and Calendar. And, later use SimpleDateFormat class to convert the date into user friendly format.

-> Date() + SimpleDateFormat()

DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));

-> get the time

Date date = new Date();
System.out.println(date.getTime());

-> get the time stamp

Date date = new Date();
System.out.println(new Timestamp(date.getTime()));

You can define various format i.e

-> dd-MMM-yy HH:mm:ss
-> yyyy/MM/dd HH:mm:ss
-> yy/MM/dd HH:mm:ss

You can use as working code like

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
 
 
public class GetCurrentDateTime {
  public static void main(String[] args) {
 
	   DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	   //get current date time with Date()
	   Date date = new Date();
	   System.out.println(dateFormat.format(date));
            
           //get the time
           Date date = new Date();
	   System.out.println(new Timestamp(date.getTime()));

           // get the timestamp
 
  }
}