package org.kodejava.util;
import java.util.*;
public class ResourceBundleToProperties {
public static void main(String[] args) {
// Load resource bundle Messages_en_GB.properties from the classpath.
ResourceBundle resource = ResourceBundle.getBundle("Messages", Locale.UK);
// Call the convertResourceBundleToProperties method to convert the resource
// bundle into a Properties object.
Properties properties = convertResourceBundleToProperties(resource);
// Print the entire contents of the Properties.
Enumeration<Object> keys = properties.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
String value = (String) properties.get(key);
System.out.println(key + " = " + value);
}
}
/**
* Convert ResourceBundle into a Properties object.
*
* @param resource a resource bundle to convert.
* @return Properties a properties version of the resource bundle.
*/
private static Properties convertResourceBundleToProperties(ResourceBundle resource) {
Properties properties = new Properties();
Enumeration<String> keys = resource.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
properties.put(key, resource.getString(key));
}
return properties;
}
}
Util Package
How do I convert ResourceBundle to Map?
The following code snippet will convert a resource bundle into a map object, a key-value mapping. It will read from a file called Messages_en_GB.properties which corresponding to the Locale.UK. For example the file contain the following string. This file should be placed under the resources directory.
welcome.message = Hello World!
package org.kodejava.util;
import java.util.*;
public class ResourceBundleToMap {
public static void main(String[] args) {
// Load resource bundle Messages_en_GB.properties from the classpath.
ResourceBundle resource = ResourceBundle.getBundle("Messages", Locale.UK);
// Call the convertResourceBundleTopMap method to convert the resource
// bundle into a Map object.
Map<String, String> map = convertResourceBundleToMap(resource);
// Print the entire contents of the Map.
for (String key : map.keySet()) {
String value = map.get(key);
System.out.println(key + " = " + value);
}
}
/**
* Convert ResourceBundle into a Map object.
*
* @param resource a resource bundle to convert.
* @return Map a map version of the resource bundle.
*/
private static Map<String, String> convertResourceBundleToMap(ResourceBundle resource) {
Map<String, String> map = new HashMap<>();
Enumeration<String> keys = resource.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
map.put(key, resource.getString(key));
}
return map;
}
}
How do I convert LinkedList to array?
package org.kodejava.util;
import java.util.LinkedList;
import java.util.List;
public class LinkedListToArray {
public static void main(String[] args) {
List<String> list = new LinkedList<>();
list.add("Blue");
list.add("Green");
list.add("Purple");
list.add("Orange");
// Converting LinkedList to array can be done by calling the toArray()
// method of the List;
String[] colors = new String[list.size()];
list.toArray(colors);
for (String color : colors) {
System.out.println("color = " + color);
}
}
}
How do I get all available timezones?
package org.kodejava.util;
import java.util.TimeZone;
public class TimezonesExample {
public static void main(String[] args) {
String[] availableTimezones = TimeZone.getAvailableIDs();
for (String timezone : availableTimezones) {
System.out.println("Timezone ID = " + timezone);
}
}
}
Some examples of the returned timezones ids are:
Timezone ID = Etc/GMT+12
Timezone ID = Etc/GMT+11
Timezone ID = MIT
Timezone ID = Pacific/Apia
Timezone ID = Pacific/Midway
Timezone ID = Pacific/Niue
Timezone ID = Pacific/Pago_Pago
Timezone ID = Pacific/Samoa
Timezone ID = US/Samoa
...
How do I get currency symbol?
package org.kodejava.util;
import java.util.Currency;
import java.util.Locale;
public class CurrencySymbol {
public static void main(String[] args) {
Currency currency = Currency.getInstance(Locale.JAPAN);
System.out.println("Japan = " + currency.getSymbol());
currency = Currency.getInstance(Locale.UK);
System.out.println("UK = " + currency.getSymbol());
currency = Currency.getInstance(Locale.US);
System.out.println("US = " + currency.getSymbol());
currency = Currency.getInstance(new Locale("in", "ID"));
System.out.println("Indonesia = " + currency.getSymbol());
}
}
The result of the code snippet above:
Japan = ¥
UK = £
US = $
Indonesia = IDR