How to Display Date in Different Formats using Java



In this tutorial, we will display dates in various formats using the DateFormat and SimpleDateFormat classes in Java. These classes provide multiple formatting options to display dates in different styles, such as short, medium, long, and custom formats.

Using DateFormat to display date in different formats

DateFormat class : A DateFormat class is used to format and parse dates. It offers several predefined date formats such as short, medium, long, and full.

A Date object (dt) is created using a specified time. Various DateFormat instances are created with different formatting styles (short, medium, long, full). These formats are then applied to the date, and the results are printed in each of these formats.

Steps

1. Create a Date object: Initialize a Date object with a specific timestamp.

Date dt = new Date(1000000000000L);

2. Create an array of DateFormat objects: Declare an array to store different DateFormat objects.

DateFormat[] dtformat = new DateFormat[6];

3. Assign different formatting styles to DateFormat objects: Assign various formatting styles (default, medium, full, long, short) to the DateFormat objects.

dtformat[0] = DateFormat.getInstance();
dtformat[1] = DateFormat.getDateInstance();
dtformat[2] = DateFormat.getDateInstance(DateFormat.MEDIUM);
dtformat[3] = DateFormat.getDateInstance(DateFormat.FULL);
dtformat[4] = DateFormat.getDateInstance(DateFormat.LONG);
dtformat[5] = DateFormat.getDateInstance(DateFormat.SHORT);

Example

The following example displays the names of the weekdays in short form with the help of DateFormatSymbols().getWeekdays() method of DateFormatSymbols class

import java.text.*;
import java.util.*;

public class Main {
   public static void main(String[] args) {
      Date dt = new Date(1000000000000L);
      DateFormat[] dtformat = new DateFormat[6];
      
      dtformat[0] = DateFormat.getInstance();
      dtformat[1] = DateFormat.getDateInstance();
      dtformat[2] = DateFormat.getDateInstance(DateFormat.MEDIUM);
      dtformat[3] = DateFormat.getDateInstance(DateFormat.FULL);
      dtformat[4] = DateFormat.getDateInstance(DateFormat.LONG);
      dtformat[5] = DateFormat.getDateInstance(DateFormat.SHORT);
      
      for(DateFormat dateform : dtformat) System.out.println(dateform.format(dt));
   }
}

Output

9/9/01 7:16 AM
Sep 9, 2001
Sep 9, 2001
Sunday, September 9, 2001
September 9, 2001
9/9/01

Using SimpleDateFormat to display date in custom formats

A SimpleDateFormat object is used to format the current date (curDate) into different custom formats. The format() method is called with different date patterns, including custom ones like "yyyy/MM/dd", "dd-M-yyyy hh:mm:ss", and "dd MMMM yyyy zzzz". After formatting, the date is printed in each format.

Example

The following is another example of date in another format

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class SimpleDateFormatExample {
   public static void main(String[] args) {
      Date curDate = new Date();
      SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
      
      String DateToStr = format.format(curDate);
      System.out.println(DateToStr);
      
      format = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
      DateToStr = format.format(curDate);
      System.out.println(DateToStr);
      
      format = new SimpleDateFormat("dd MMMM yyyy zzzz", Locale.ENGLISH);
      DateToStr = format.format(curDate);
      System.out.println(DateToStr);
      
      format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
      DateToStr = format.format(curDate);
      System.out.println(DateToStr);
      
      try {
         Date strToDate = format.parse(DateToStr);
         System.out.println(strToDate);
      } catch (ParseException e) {
         e.printStackTrace();
      } 
   } 
}

Output

2024/12/09
09-12-2024 07:50:04
09 December 2024 Greenwich Mean Time
Mon, 09 Dec 2024 07:50:04 GMT
Mon Dec 09 07:50:04 GMT 2024
java_date_time.htm
Advertisements