How to get current date and time in Java

You can get current date and time via Date and Calendar Class. As we are displaying date in the specific format we have used SimpleDateFormat class.

package com.kruders;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CurrentDateTime {
    public static void  main(String arg[]) {
      //using Date Class
      DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      Date date = new Date();
      System.out.println("Using Date Class " + dateFormat.format(date));
      //using Calendar Class
      Calendar cal = Calendar.getInstance();
      System.out.println("Using Calendar Class " + dateFormat.format(cal.getTime()));

When you run the above example you’ll get an output like:

Using Date Class 2012/08/09 15:13:36
Using Calendar Class 2012/08/09 15:13:36

No comments yet.

Leave a Reply