Formatting how data should be displayed on the screen is a common requirement when creating a program or application. Displaying information in a good and concise format can be an added values to the users of the application. In the following code snippet we will learn how to format a date into a certain display format.
For these purposes we can utilize the DateFormat
and SimpleDateFormat
classes from the java.text
package. We can easily format a date in our program by creating an instance of SimpleDateFormat
class and specify to format pattern. Calling the DateFormat.format(Date date)
method will format a date into a date-time string.
You can see the details about date and time patters in the following link: Date and Time Patterns. Now, let’s see an example as shown in the code snippet below.
package org.kodejava.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 : 24/09/2021
Today : Fri, 24/09/2021
Today : Fri, 24 Sep 2021
Today : Friday, 24 September 2021, 09:36:32.724 AM
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024
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.SimpleDateFormat
to 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!
How can I get current date in MMM DD, YYYY format?
Hi Mahesh,
The
java.util.Date
object doesn’t have any format on its own. If you want to display it in certain format you can use thejava.text.SimpleDateFormat
as the example in the above code snippet shows.How to change a date 19/09/1999 to MM/dd/YYYY format?
Hi Anand,
You can do it like this:
Hi
How can i change 2024-11-04 to 04-Nov-24 format?
Hi Swati,
Here is an example to convert the date using Java Date Time API: