Format date with DateFormat.MEDIUM in Java


DateFormat.MEDIUM is a constant for medium style pattern.

Firstly, we will create date object −

Date dt = new Date();
DateFormat dateFormat;

Let us format date for different locale with DateFormat.MEDIUM −

// CHINESE
dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINESE);
// CANADA
dateFormat = DateFormat.getDateInstance(DateFormat. MEDIUM, Locale.CANADA);

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 MEDIUM constant
      dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.FRENCH);
      System.out.println("Locale FRENCH = " + dateFormat.format(dt));
      dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.GERMANY);
      System.out.println("Locale GERMANY = " + dateFormat.format(dt));
      dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINESE);
      System.out.println("Locale CHINESE = " + dateFormat.format(dt));
      dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CANADA);
      System.out.println("Locale CANADA = " + dateFormat.format(dt));
      dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ITALY);
      System.out.println("Locale ITALY = " + dateFormat.format(dt));
      dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.TAIWAN);
      System.out.println("Locale TAIWAN = " + dateFormat.format(dt));
   }
}

Output

Locale FRENCH = 22 nov. 2018
Locale GERMANY = 22.11.2018
Locale CHINESE = 2018-11-22
Locale CANADA = 22-Nov-2018
Locale ITALY = 22-nov-2018
Locale TAIWAN = 2018/11/22
raja
Published on 21-Dec-2018 11:31:20
Advertisements