java print current date and time with seconds code example

Example 1: How to get current date and time in java

// get current date and timein java using Calendar.getTime() method
import java.util.Calendar;
import java.util.Date;
public class UsingCalendarClass
{
   public static void main(String[] args)
   {
      Date date = Calendar.getInstance().getTime();
      System.out.println(date);
   }
}

Example 2: java get current date without time

Date currentDate = new Date();
  SimpleDateFormat dateFormat= new SimpleDateFormat("dd/MMM/yyyy");
  String dateOnly = dateFormat.format(currentDate);
  dateTextView.setText(dateOnly);

Tags:

Java Example