Internationalizing Date (I18N with Date)

Internationalizing Date (I18N with Date)

  1. Internationalizing Date
  2. Commonly used methods of the DateFormat class
  3. Example of Inernationalizing Date

The format of the dates differ from one region to another that is why we internationalize the dates.

We can internationalize the date by using the getDateInstance() method of the DateFormatclass. It receives the locale object as a parameter and returns the instance of the DateFormat class.


Commonly used methods of DateFormat class for internationalizing date

There are many methods of the DateFormat class. Let’s see the two methods of the DateFormat class for internationalizing the dates.

  • public static DateFormat getDateInstance(int style, Locale locale) returns the instance of the DateFormat class for the specified style and locale. The style can be DEFAULT, SHORT, LONG etc.
  • public String format(Date date) returns the formatted and localized date as a string.

Example of Internationalizing Date

In this example, we are displaying the date according to the different locale such as UK, US, FRANCE etc. For this purpose we have created the printDate() method that receives Locale object as an instance. The format() method of the DateFormat class receives the Date object and returns the formatted and localized date as a string.

  1. import java.text.DateFormat;
  2. import java.util.*;
  3. public class InternationalizationDate {
  4. static void printDate(Locale locale){
  5. DateFormat formatter=DateFormat.getDateInstance(DateFormat.DEFAULT,locale);
  6. Date currentDate=new Date();
  7. String date=formatter.format(currentDate);
  8. System.out.println(date+” “+locale);
  9. }
  10. public static void main(String[] args) {
  11.     printDate(Locale.UK);
  12.     printDate(Locale.US);
  13.     printDate(Locale.FRANCE);
  14. }
  15. }
download this example
Output:01-Mar-2012 en_GB
       Mar 1, 2012 en_US
       1 mars 2012 fr_FR

Next TopicInternationalizing Time
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Internationalizing Date (I18N with Date) Internationalizing Date Commonly used methods of the DateFormat class Example of Inernationalizing Date The format of the dates differ from one region to another that is why we internationalize the dates. We can internationalize the date by using the getDateInstance() method of the DateFormatclass. It receives the locale object as a parameter and […]

Tags:

....
...