Syntax error Format time with DateFormat.SHORT in Java

Format time with DateFormat.SHORT in Java



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

Firstly, we will create Date object

Date dt = new Date();
DateFormat dateFormat;

Let us format time for different locale with DateFormat.SHORT

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

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

Output

Locale FRENCH = 09:45
Locale GERMANY = 09:45
Locale CHINESE = ??9:45
Locale CANADA = 9:45 AM
Locale ITALY = 9.45
Locale TAIWAN = ?? 9:45
Updated on: 2020-06-27T12:58:35+05:30

565 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements