Format date with DateFormat.SHORT in Java

Java 8Object Oriented ProgrammingProgramming

DateFormat.SHORT is a constant for short style pattern.

Firstly, we will create date object

Date dt = new Date();
DateFormat dateFormat;

Let us format date for different locale with DateFormat.SHORT

// FRENCH
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.FRENCH);
// GERMANY
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMANY);

The following is an example −

Example

 Live Demo

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class Demo {
   public static void main(String args[]) {
      Date dt = new Date();
      DateFormat dateFormat;
// Date Format SHORT constant
      dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.FRENCH);
      System.out.println("Locale FRENCH = " + dateFormat.format(dt));
      dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMANY);
      System.out.println("Locale GERMANY = " + dateFormat.format(dt));
      dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINESE);
      System.out.println("Locale CHINESE = " + dateFormat.format(dt));
      dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.CANADA);
      System.out.println("Locale CANADA = " + dateFormat.format(dt));
      dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ITALY);
      System.out.println("Locale ITALY = " + dateFormat.format(dt));
      dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.TAIWAN);
      System.out.println("Locale TAIWAN = " + dateFormat.format(dt));
   }
}

Output

Locale FRENCH = 22/11/18
Locale GERMANY = 22.11.18
Locale CHINESE = 18-11-22
Locale CANADA = 22/11/18
Locale ITALY = 22/11/18
Locale TAIWAN = 2018/11/22
raja
Published on 19-Dec-2018 10:21:31
Advertisements