Format date with DateFormat.LONG in Java


DateFormat.LONG is a constant for long style pattern.

Firstly, we will create date object −

Date dt = new Date();
DateFormat dateFormat;

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

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

Output

Locale FRENCH = 22 novembre 2018
Locale GERMANY = 22. November 2018
Locale CANADA = November 22, 2018
Locale ITALY = 22 novembre 2018

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements