Java program to get current date now

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:
GetDateNow.java
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class GetDateNow {
public static void main(String arg[]) {
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter=
new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
String dateNow = formatter.format(currentDate.getTime());
System.out.println("Now the date is :=> " + dateNow);
}
}
|
Output:
C:\javaexamples>javac GetDateNow.java
C:\javaexamples>java GetDateNow
Now the date is :=> 2008/Oct/18 13:14:56 |
Download Source Code

|