In this section of simple date formatter example we are going to describe you how you can use different constructors of SimpleDateFormat class to convert the date into the specified format. There are various constructors of SimpleDateFormat class.
Date todaysDate = new java.util.Date(); creates a new instance of Date with the current date and time value.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = formatter.format(todaysDate);
Above lines of code formats the date object into the "yyyy-MM-dd HH:mm:ss" format.
formatter = new SimpleDateFormat("yyyy-MM-dd");
formattedDate = formatter.format(todaysDate);
In the above code we have formatted the date into "yyyy-MM-dd" format.
Here is the full example code of SimpleFormatDate.java as follows:
Save SimpleFormatDate.java and compile it with the javac command.
Formatted date is ==>2008-10-10 13:03:54
Formatted date is ==>2008-10-10
Formatted date is ==>10/10/2008
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.