Java Utililty Methods Locale Format

List of utility methods to do Locale Format

Description

The list of methods to do Locale Format are organized into topic(s).

Method

StringformatPCT(Object num)
Format percentage to two decimal places, like xx.xx%.
String result;
if (num instanceof Number) {
    Locale en = new Locale("en");
    DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(en);
    df.applyPattern("0.00");
    result = df.format(num) + "%";
} else {
    result = num.toString();
...
StringformatPersent(Object input)
DOC Zqin Comment method "formatPersent".
if (checkInput(input)) {
    Double db = new Double(input.toString());
    DecimalFormat format = (DecimalFormat) DecimalFormat.getPercentInstance(Locale.ENGLISH);
    format.applyPattern("0.00%"); 
    return format.format(db);
return null;
StringformatSmartDate(Date date)
Format a date as returned for producing a portable player readable date.
DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US);
try {
    SimpleDateFormat simpleFormatter = (SimpleDateFormat) formatter;
    simpleFormatter.applyPattern("yy-MM-dd");
    String result = simpleFormatter.format(date);
    return result;
} catch (Exception e) {
return "";
StringformatString(Object[] args)
This method will format the message contained in the first element of the supplied array in the default locale
return formatString(args, null);
StringformatString(String pattern, Object... args)
Format using MesssageFormat but with the ROOT locale
return new MessageFormat(pattern, Locale.ROOT).format(args);
DateformatString2Date(String dateString)
format String Date
SimpleDateFormat sdf = new SimpleDateFormat();
Date datum = new Date();
try {
    datum = sdf.parse(dateString);
    return datum;
} catch (ParseException e) {
    return null;
StringformattedFromDouble(double d, int scale, Locale locale)
Convert from a BigDecimal forcing the scale.
NumberFormat formatted = NumberFormat.getNumberInstance(locale);
formatted.setMaximumFractionDigits(scale);
formatted.setMinimumFractionDigits(scale);
return formatted.format(d);
StringformattedFromLong(long l, Locale locale)
Convert from an long.
return NumberFormat.getNumberInstance(locale).format(l);
doubleformattedToDouble(String str, Locale loc)
Convert to double from a formatted string.
if (str.length() == 0) {
    str = "0";
return NumberFormat.getNumberInstance(loc).parse(str).doubleValue();
StringformatTime(Calendar calendar, String format)
format Time
return new SimpleDateFormat(format, Locale.getDefault()).format(calendar.getTime());