«Back to Home

Core Java

Topics

Internationalize Time In Java

Internationalize Time
 
The display format of the time is different from one region to another region. Thus, we need to internationalize the time. The DateFormat class gives some methods to internationalize the time. The getTimeInstance() method of the DateFormat class is used to return the object of the DateFormat class for the specific style and locale.
 
Syntax of getTimeInstance() method

public static DateFormat getTimeInstance(int style, Locale locale)
 
Let’s see an example to Internationalize the time.
 
Code
  1. import java.text.DateFormat;  
  2. import java.util.*;  
  3. public class TimeFormatExample {  
  4.     static void printTime(Locale locale) {  
  5.         DateFormat f = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale);  
  6.         Date cDate = new Date();  
  7.         String time = f.format(cDate);  
  8.         System.out.println(time + " in " + locale);  
  9.     }  
  10.     public static void main(String[] args) {  
  11.         printTime(Locale.US);  
  12.         printTime(Locale.FRANCE);  
  13.     }  
  14. }  
4

Output

5

In the example, mentioned above, we display the current time for the particular locale. The format() method of the DateFormat class gets the date object, displays the formatted and localized time as a string.
 
Summary

Thus, we learnt that the getTimeInstance() method of the DateFormat class is used to return the object of the DateFormat class for the specific style and locale. We also learnt how to internationalize the time in Java.