DateFormat and Locale
In this chapter you will learn:
Get all locales
static Locale[] getAvailableLocales()
Returns all locales for which the get*Instance methods of this class can return localized instances.
import java.text.DateFormat;
import java.util.Locale;
// ja va 2 s . c om
public class Main{
public static void main(String args[]) {
Locale[] locales = DateFormat.getDateInstance().getAvailableLocales();
for(Locale l: locales){
System.out.println(l.getDisplayCountry());
}
}
}
The output:
Use Locale with DateFormat
Locale
class stores country specific information. We can use locale
to represent different countries. For example,
Locale.UK
is for the United Kindom, Locale.US
represents America. DateFormat
is smart enough to format date for each country
by looking at the Locale information passed in.
The following code uses the DateFormat
together with the Locale
and outputs
date information in different format for each different country.
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
/*from jav a2 s .c o m*/
public class MainClass {
public static void main(String args[]) {
Date date = new Date();
DateFormat df;
df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.JAPAN);
System.out.println("Japan: " + df.format(date));
df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.KOREA);
System.out.println("Korea: " + df.format(date));
df = DateFormat.getDateInstance(DateFormat.LONG, Locale.UK);
System.out.println("United Kingdom: " + df.format(date));
df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
System.out.println("United States: " + df.format(date));
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Date, Time, Calendar, TimeZone