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;
|
Output:
| C:\javaexamples>javac GetDateNow.java C:\javaexamples>java GetDateNow Now the date is :=> 2008/Oct/18 13:14:56 |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Java program to get current date now
Post your Comment