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)
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.
import java.text.DateFormat; import java.util.*; public class InternationalizingTime { static void printTime(Locale locale){ DateFormat formatter=DateFormat.getTimeInstance(DateFormat.DEFAULT,locale); Date currentDate=new Date(); String time=formatter.format(currentDate); System.out.println(time+" in locale "+locale); } public static void main(String[] args) { printTime(Locale.UK); printTime(Locale.US); printTime(Locale.FRANCE); } }