Formatting a display is a common requirement when creating a program. Information in a good format can be seen as an added value to the user of the program. Using Java SimpleDateFormat we can easily format a date in our program.
package org.kodejava.example.text;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateFormatExample {
public static void main(String[] args) {
Date date = Calendar.getInstance().getTime();
// Display a date in day, month, year format
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String today = formatter.format(date);
System.out.println("Today : " + today);
// Display date with day name in a short format
formatter = new SimpleDateFormat("EEE, dd/MM/yyyy");
today = formatter.format(date);
System.out.println("Today : " + today);
// Display date with a short day and month name
formatter = new SimpleDateFormat("EEE, dd MMM yyyy");
today = formatter.format(date);
System.out.println("Today : " + today);
// Formatting date with full day and month name and show time up to
// milliseconds with AM/PM
formatter = new SimpleDateFormat("EEEE, dd MMMM yyyy, hh:mm:ss.SSS a");
today = formatter.format(date);
System.out.println("Today : " + today);
}
}
Let’s view what we got on the console:
Today : 28/01/2018
Today : Sun, 28/01/2018
Today : Sun, 28 Jan 2018
Today : Sunday, 28 January 2018, 03:09:43.293 PM
Latest posts by Wayan Saryada (see all)
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020
- How do I get a list of all TimeZones Ids using Java 8? - April 25, 2020
- How do I get HTTP headers using HttpClient HEAD request? - April 22, 2020

Nice article. For more Date and Calendar examples please go through this blog http://adnjavainterview.blogspot.in/2014/08/java-date-and-calendar-examples.html.
How can I convert string to date in yyyy-mm-dd format in java? Thank you.
Hi Hsu,
You can use the
parse()method of thejava.text.SimpleDateFormatto convert string to date. See the following snippets:But this script again give result in this format Fri Feb 28 00:00:00 UTC 2020
Also you can also call only the constructor and make one line of code:
Nice article!