Format time with DateFormat.FULL in Java

Java 8Object Oriented ProgrammingProgramming

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

Firstly, we will create Date object

Date dt = new Date();
DateFormat dateFormat;

Let us format time for different locale with DateFormat.FULL

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

Output

Locale FRENCH = 09 h 52 UTC
Locale GERMANY = 09:52 Uhr UTC
Locale CHINESE = 上午09时52分46秒 UTC
Locale CANADA = 9:52:46 o'clock AM UTC
Locale ITALY = 9.52.46 UTC
Locale TAIWAN = 上午09時52分46秒 UTC
raja
Published on 19-Dec-2018 06:17:17
Advertisements