In this example program you will learn how to get the current date and time in Java program.In this example program you will learn how to get the current date and time in Java program.
In this example program we have to get the current date at present now. We have used Calendar class for getting the current date and time instance. For printing date in the specific format we have used SimpleDateFormat class of "java.text" package.
Calendar currentDate = Calendar.getInstance(); gets current date instance. To show this in the specific format we can use SimpleDateFormat class.
SimpleDateFormat formatter= new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
String dateNow = formatter.format(currentDate.getTime());
Above lines of code gets the current date and time in the yyyy/MMM/dd HH:mm:ss format.
Here is the full example code of GetDateNow.java as follows:
In this above program we have used the Calendar.getInstance() method of the Calendar class. This method is used to get the calendar instance using the default specified time zone and the specified locale. You can also specify the time zone and the locale while getting the instance of the calendar.
Here is the Declaration of the Calendar.getInstance() method:
public static Calendar getInstance(TimeZone zone,Locale locale)
Parameters of the Calendar.getInstance() method:
In the above program we have also used the SimpleDateFormat class for format the date. If you run the program you will get the following output:
Now the date is :=> 2008/Oct/18 13:14:56