Java Program to format time with DateFormat.LONG

Java 8Object Oriented ProgrammingProgramming

Use the getTimeInstance() method in Java to get the time format for the locale you have set. DateFormat.LONG is a constant for long style pattern.

Firstly, we will create Date object

Date dt = new Date();
DateFormat dateFormat;

Let us format time for different locale with DateFormat.LONG

dateFormat = DateFormat.getTimeInstance(DateFormat.LONG, Locale.FRENCH);
System.out.println("Locale FRENCH = " + dateFormat.format(dt));
dateFormat = DateFormat.getTimeInstance(DateFormat.LONG, Locale.GERMANY);
System.out.println("Locale GERMANY = " + dateFormat.format(dt));
dateFormat = DateFormat.getTimeInstance(DateFormat.LONG, Locale.CHINESE);
System.out.println("Locale CHINESE = " + dateFormat.format(dt));

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

Output

Locale FRENCH = 09:50:40 UTC
Locale GERMANY = 09:50:40 UTC
Locale CHINESE = 上午09时50分40秒
Locale CANADA = 9:50:40 UTC AM
Locale ITALY = 9.50.40 UTC
Locale TAIWAN = 上午09時50分40秒
raja
Updated on 27-Jun-2020 12:55:38

Advertisements