Internationalize Time In Java
Internationalize Time
The display format of the time is different from one region to another region. Thus, we need to internationalize the time. The DateFormat class gives some methods to internationalize the time. The getTimeInstance() method of the DateFormat class is used to return the object of the DateFormat class for the specific style and locale.
Syntax of getTimeInstance() method
public static DateFormat getTimeInstance(int style, Locale locale)
Let’s see an example to Internationalize the time.
Code
- import java.text.DateFormat;
- import java.util.*;
- public class TimeFormatExample {
- static void printTime(Locale locale) {
- DateFormat f = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale);
- Date cDate = new Date();
- String time = f.format(cDate);
- System.out.println(time + " in " + locale);
- }
- public static void main(String[] args) {
- printTime(Locale.US);
- printTime(Locale.FRANCE);
- }
- }
Output
In the example, mentioned above, we display the current time for the particular locale. The format() method of the DateFormat class gets the date object, displays the formatted and localized time as a string.
Summary
Thus, we learnt that the getTimeInstance() method of the DateFormat class is used to return the object of the DateFormat class for the specific style and locale. We also learnt how to internationalize the time in Java.