Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

I18N with 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. public class InternationalizingTime {
  4. static void printTime(Locale locale){
  5. DateFormat formatter=DateFormat.getTimeInstance(DateFormat.DEFAULT,locale);
  6. Date currentDate=new Date();
  7. String time=formatter.format(currentDate);
  8. System.out.println(time+” in locale “+locale);
  9. }
  10. public static void main(String[] args) {
  11. printTime(Locale.UK);
  12. printTime(Locale.US);
  13. printTime(Locale.FRANCE);
  14. }
  15. }
Output:16:22:49 in locale en_GB
4:22:49 PM in locale en_US
16:22:49 in locale fr_FR

Comments

comments