📄 utilities.java
字号:
/*
* @author : Neelesh
* @Version : 2.0
*
* Development Environment : Oracle9i JDeveloper
* Name of the File : Utilities.java
* Creation/Modification History :
*
* Neelesh 06-Jan-2003 Created
*
*/
package oracle.otnsamples.util;
// Exception class
import java.io.IOException;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Locale;
// Java utility classes
import java.util.Properties;
import java.util.ResourceBundle;
/**
* This class implements a set of utility methods used by other modules of
* virtual shopping mall application.
*
* @author Neelesh
* @version 2.0
*/
public final class Utilities {
// Default Minimum Fractional digits
private static final int MIN_DIGITS = 2;
// Default Maximum Fractional digits
private static final int MAX_DIGITS = 2;
// Default date format
private static final String DATEFORMAT = "MM/dd/yyyy";
/**
* Method to format a date object to a string of supplied format If the
* format is null, the default mm-dd-yyyy is assumed Returns null if the
* format string is invalid.
*
* @param <b>date </b>The date object to be formatted
* @param <b>format </b> The date format
*
* @return <b>date</b> The string representation of date
*/
public static String format(Date date, String format) {
return format(date, format, null);
}
/**
* Method to format a date object to a string of supplied format and locale
* If the format is null, the default mm-dd-yyyy is assumed if locale is
* null, the default locale of the system is assumed Returns null if the
* format string is invalid.
*
* @param <b>date </b>The date object to be formatted
* @param <b>format </b> The date format
* @param <b>locale </b> The locale
*
* @return <b>date</b> The string representation of date
*/
public static String format(Date date, String format, Locale locale) {
try {
if(format == null) {
format = DATEFORMAT;
}
if(locale == null) {
locale = Locale.getDefault();
}
SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
return sdf.format(date);
} catch(Exception ex) {
throw new UtilityException("Exception thrown from format " +
"method of Utilities class : " +
ex.getMessage());
}
}
/**
* Method to format a double to a currency based on supplied locale. If
* locale is null, the default locale of the system is assumed Returns
* String representation of the value passed if an error is encountered.
*
* @param <b>value </b>The double value to be formatted
* @param <b>locale </b> The locale
*
* @return <b>String</b> The formatted string representation of double value
*/
public static String currencyFormat(double value, Locale locale) {
try {
return NumberFormat.getCurrencyInstance((locale == null)
? Locale.getDefault() : locale)
.format(value);
} catch(Exception ex) {
return String.valueOf(value);
}
}
/**
* Method to format a double to a string based on supplied locale. If locale
* is null, the default locale of the system is assumed Returns default
* String representation of the value passed if an error is encountered.
*
* @param <b>value </b>The double value to be formatted
* @param <b>locale </b> The locale
*
* @return <b>String</b> The formatted string representation of double value
*/
public static String format(double value, Locale locale) {
try {
return NumberFormat.getInstance((locale == null) ? Locale.getDefault()
: locale).format(value);
} catch(Exception ex) {
return String.valueOf(value);
}
}
/**
* Method to round a double value to the given precision.
*
* @param <b>val </b> The double to be rounded
* @param <b>precision </b> Rounding precision
*
* @return <b>double</b> The rounded value
*/
public static double round(double val, int precision) {
// Multiply by 10 to the power of precision and add 0.5 for rounding up
// Take the nearest integer smaller than this value
val = Math.floor((val * Math.pow(10, precision)) + 0.5);
// Divide it by 10**precision to get the rounded value
return val / Math.pow(10, precision);
}
/**
* Method to format a double value to a string.
*
* @param <b>value </b>The date object to be formatted
* @param <b>minDigits </b>The date object to be formatted
* @param <b>maxDigits </b>The date object to be formatted
*
* @return <b>value</b> The string representation of the double value passed
*/
public static String format(double value, int minDigits, int maxDigits) {
// Number format class to format the values
NumberFormat numFormat = NumberFormat.getInstance();
numFormat.setMinimumFractionDigits(minDigits);
numFormat.setMaximumFractionDigits(maxDigits);
return numFormat.format(value);
}
/**
* This method reads a properties file, which is passed as the parameter to
* it and load it into a java Properties object and returns it.
*
* @param <b>file </b> Represents the .properties file
*
* @return <b>Properties</b>The properties object
*
* @throws <b>IOExceptionIOException </b> The exception this method can throw
*/
public static Properties loadParams(String file)
throws IOException {
// Loads a ResourceBundle and creates Properties from it
Properties prop = new Properties();
ResourceBundle bundle = ResourceBundle.getBundle(file);
Enumeration enum = bundle.getKeys();
String key = null;
while(enum.hasMoreElements()) {
key = (String) enum.nextElement();
prop.put(key, bundle.getObject(key));
}
return prop;
}
/**
* This method reads a properties file, which is passed as the parameter to
* it and load it into a java Properties object and returns it.
*
* @param <b>file </b> Represents the .properties file
*
* @return <b>Properties</b>The properties object
*
* @throws <b>IOExceptionIOException </b> The exception this method can throw
*/
public static Properties loadParams(String file, String language)
throws IOException {
// Loads a ResourceBundle and creates Properties from it
Properties prop = new Properties();
ResourceBundle bundle =
ResourceBundle.getBundle(file, new Locale(language, ""));
Enumeration enum = bundle.getKeys();
String key = null;
while(enum.hasMoreElements()) {
key = (String) enum.nextElement();
prop.put(key, bundle.getObject(key));
}
return prop;
}
/**
* Method to parse a date string in the format mm-dd-yyyy to a Date object
* This method returns null if the input string does not match the format.
*
* @param <b>dateString </b> The string to be converted to a Date
*
* @return <b>Date</b>The parsed date object
*/
public static Date parse(String dateString) {
return parse(dateString, null);
}
/**
* Method to parse a date string in the supplied format to a Date object. If
* the format is null, the default mm-dd-yyyy is assumed and returns null if
* the input string does not match the format.
*
* @param <b>dateString </b> The string to be converted to a Date
* @param <b>formatString </b> The format of the string
*
* @return <b>Date</b> The Date object of the string parsed
*/
public static Date parse(String dateString, String formatString) {
try {
if(formatString == null) {
formatString = DATEFORMAT;
}
SimpleDateFormat sdf = new SimpleDateFormat(formatString);
return sdf.parse(dateString);
} catch(Exception ex) {
throw new UtilityException("Exception thrown from parse " +
"method of Utilities class of given " +
"status : " + ex.getMessage());
}
}
/**
* Method to format a date object to a mm-dd-yyyy string.
*
* @param <b>date </b>The date object to be formatted
*
* @return <b>date</b> String representation of date
*/
public static String format(Date date) {
return format(date, null);
}
/**
* Method to format a double value to a string.
*
* @param <b>value </b>The date object to be formatted
*
* @return <b>String</b> The string representation of the double value
*/
public static String format(double value) {
// Number format class to format the values
NumberFormat numFormat = NumberFormat.getInstance();
numFormat.setMinimumFractionDigits(MIN_DIGITS);
numFormat.setMaximumFractionDigits(MAX_DIGITS);
return numFormat.format(value);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -