I18N with Time

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Internationalizing Time (I18N with Time)

  1. Internationalizing Time

  2. Example of Internationalizing Time

The display format of the time differs from one region to another, so we need to internationalize the time.

For internationalizing the time, the DateFormat class provides some useful methods.

The getTimeInstance() method of the DateFormat class returns the instance of the DateFormat class for the specified style and locale.

Syntax of the getTimeInstance() method is given below:

public static DateFormat getTimeInstance(int style, Locale locale)

Example of Internationalizing Time

In this example, we are displaying the current time for the specified locale. The format() method of the DateFormat class receives date object and returns the formatted and localized time as a string. Notice that the object of Date class prints date and time both.

  1. import java.text.DateFormat;  
  2. import java.util.*;  
  3.   
  4. public class InternationalizingTime {  
  5.   
  6. static void printTime(Locale locale){  
  7. DateFormat formatter=DateFormat.getTimeInstance(DateFormat.DEFAULT,locale);  
  8. Date currentDate=new Date();  
  9. String time=formatter.format(currentDate);  
  10. System.out.println(time+" in locale "+locale);  
  11. }  
  12.   
  13. public static void main(String[] args) {  
  14. printTime(Locale.UK);  
  15. printTime(Locale.US);  
  16. printTime(Locale.FRANCE);  
  17. }  
  18. }  
Output:16:22:49 in locale en_GB
4:22:49 PM in locale en_US
16:22:49 in locale fr_FR