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;
}
}
Category Archives: Core API
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 check if a string is a valid date?
The following code can be used to validate if a string contains a valid date information. The pattern of the date is defined by the java.text.SimpleDateFormat object. When the date is not valid a java.text.ParseException will be thrown.
package org.kodejava.text;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class DateValidation {
public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
// Input to be parsed should strictly follow the defined date format
// above.
format.setLenient(false);
String date = "29/18/2021";
try {
format.parse(date);
} catch (ParseException e) {
System.out.println("Date " + date + " is not valid according to " +
format.toPattern() + " pattern.");
}
}
}
The result of the above date validation code is:
Date 29/18/2021 is not valid according to dd/MM/yyyy pattern.
How do I get Java Home directory?
To get Java Home directory we can obtain it from system properties using the java.home key.
package org.kodejava.lang;
public class JavaHomeDirectory {
public static void main(String[] args) {
String javaHome = System.getProperty("java.home");
System.out.println("JAVA HOME = " + javaHome);
}
}
On my computer this code give me the following output:
JAVA HOME = C:\Program Files\Java\jdk-17
How do I get the username of operating system active user?
This example show you how to get operating system active user’s login name. We can obtain the username of current user by reading system properties using the user.name key.
package org.kodejava.lang;
public class GettingUserName {
public static void main(String[] args) {
String username = System.getProperty("user.name");
System.out.println("username = " + username);
}
}
