java tutorial - Internationalizing Date (I18N with Date) - java programming - learn java - java basics - java for beginners



  • Internationalize the date by using the getDateInstance() method of the DateFormat class.
  • It receives the locale object as a parameter and returns the instance of the DateFormat class.

Methods of DateFormat class for internationalizing date

  • There are many methods of the DateFormat class.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

  • 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.
import java.text.DateFormat;  
import java.util.*;  
public class I18NDATE {  
      
static void printDate(Locale locale){  
DateFormat formatter=DateFormat.getDateInstance(DateFormat.DEFAULT,locale);  
Date currentDate=new Date();  
String date=formatter.format(currentDate);  
System.out.println(locale+" "+date);  
}  
  
public static void main(String[] args) {  
    printDate(Locale.UK);  
    printDate(Locale.US);  
    printDate(Locale.FRANCE);  
}  
}  
click below button to copy the code. By - java tutorial - team

Output

en_GB 23-Dec-2017
en_US Dec 23, 2017
fr_FR 23 déc. 2017

Related Searches to Internationalizing Date (I18N with Date)