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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 27-Jun-2020

334 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements