DateFormat class provides various formats to format the date. Following is list of some of the formats.

  • DateFormat.DEFAULT
  • DateFormat.SHORT
  • DateFormat.MEDIUM
  • DateFormat.LONG
  • DateFormat.FULL

Example

In following example we’ll show how to use different formats.

Open Compiler

importjava.text.DateFormat;importjava.util.Date;publicclassI18NTester{publicstaticvoidmain(String[] args){DateFormat dateFormat =DateFormat.getDateInstance(DateFormat.DEFAULT);System.out.println(dateFormat.format(newDate()));

      dateFormat =DateFormat.getDateInstance(DateFormat.SHORT);System.out.println(dateFormat.format(newDate()));

      dateFormat =DateFormat.getDateInstance(DateFormat.MEDIUM);System.out.println(dateFormat.format(newDate()));

      dateFormat =DateFormat.getDateInstance(DateFormat.LONG);System.out.println(dateFormat.format(newDate()));

      dateFormat =DateFormat.getDateInstance(DateFormat.FULL);System.out.println(dateFormat.format(newDate()));}}

Output

It will print the following result.

Jun 7, 2024
6/7/24
Jun 7, 2024
June 7, 2024
Friday, June 7, 2024

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *