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



  • The display format of the time differs from one place 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

public static DateFormat getTimeInstance(int style, Locale locale)
click below button to copy the code. By - java tutorial - team

Sample Code

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

Output

Locale en_GB in time 10:27:56
Locale en_US in time 10:27:56 AM
Locale fr_FR in time 10:27:56

Related Searches to Internationalizing Time (I18N with Time)