A Java Locale String exists of three different parts, name these parts.
What are the four ways to create a Locale object in Java?
java.util.Locale.Builder
new Locale.Builder().setLanguage(“nl”).setRegion(“NL”).build();
java.util.Locale new Locale(String language); new Locale(String language, String country); new Locale(String language, String country, String variant)
forLanguageTag factory method
Locale.forLanguageTag(“nl-NL”);
Locale Constants
Locale.CANADA_FRENCH;
How to get the default Locale of the JVM?
Locale.getDefault();
How to set the default Locale of the JVM?
Locale.setDefault(Locale locale);
How to retrieve a Resource bundle for the nl-NL locale for the bundle named MyApp?
Locale locale = new Locale("nl", "NL");
ResourceBundle.getBundle("MyApp", locale);Given the following classes what is the result:
Locale.setDefault(Locale.forLanguageTag(“nl”);
public void MyResources extends ResourceBundle {}
public void MyResources_nl extends MyResources {}
ResourceBundle.getBundle(“MyResources”, “de”);
getBundle() will return MyResources_nl
It will first try to find a MyResources class for the de language. It can’t find one so it tries the default “nl”. It can find a class. If MyResources_nl did not exist it would have returned the MyResources class. If the MyResources class did not exist it would throw a MissingResourceException.
ResourceBundle has two subclasses name these two and their uses.
Given the following classes are in package foo.bar and the following code. What is the result?
public void MyResources extends ResourceBundle {}
public void MyResources_nl extends MyResources {}
ResourceBundle.getBundle(“MyResources”, “de”);
The ResourceBundle can’t be found. You should use the fullly qualified name for the bundle: foo.bar.MyResources
What is the order ResourceBundle.getBundle uses to search for the requested Bundle?
Given the following classes and code what is the Result?
public void MyResources extends ResourceBundle {}
ResourceBundle.getBundle(“MyResource”, new Locale(“nl”));
Throws a MissingResourceException MyResource is not a known resource.
Given the following classes and code what is the Result?
public void MyResources extends ResourceBundle {}
And a properties file: MyResource.properties
ResourceBundle.getBundle(“MyResources”, new Locale(“nl”));
What is the type of the ResourceBundle?
MyResources.class
getBundle will always first look for an Class before looking for an properties file. If only a properties file is found the returned type would have been: PropertyResourceBundle
Given the following integer how can you format it using the “us” locale?
int i = 9_000_000;
Locale l = Locale.US;
NumberFormat formatter = NumberFormat.getNumberInstance(l);
formatter.format(i);
Given the following integer how can you format it as money for the “us” locale?
int i = 9_000_000;
Locale l = Locale.US;
NumberFormat formatter = NumberFormat.geCurrencyInstance(l);
formatter.format(i);
Given a date object how can you print it using the “us” locale?
Date d = new Date(); DateFormat formatter = DateFormat.getDateInstance(Locale.US); formatter.format(d);
Given a date object how can you print it in the format yyyy MMMM dd for the “us” locale?
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMMM dd", Locale.US);
sdf.format(d);Given the following code, what is the result?
Locale l = Locale.getDefault(); DateFormat df = DateFormat.getDateInstance(l); System.out.println(l.getCountry()+” “+ df.format(dt));
Wont compile. DateFormat doest not have a method getDateInstance(Local locale);
Valid arguments for getDateInstance are:
getDateInstance();
getDateInstance(int style);
getDateInstance(int style, Local locale);
Consider the following code.
public class TestClass {
public static void main(String[] args) throws Exception{
Date d = new Date();
DateFormat //1 INSERT CODE HERE
String s = //2 INSERT CODE HERE
System.out.println(s);
}
} What should be inserted at //1 and //2 above so that it will print the date in default date format for the UK Locale?
df = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.UK); and df.format(d);