| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- package commons.utility;
- import java.text.DecimalFormat;
- import java.text.DecimalFormatSymbols;
- import java.text.NumberFormat;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import javax.xml.crypto.dsig.TransformException;
- import org.apache.commons.lang.StringUtils;
- import commons.utility.database.NullValue;
- /**
- * @author basluc
- *
- * Classe generica per le formattazioni
- */
- public final class FormatUtility
- {
- static public String formatText(String text)
- {
- return (null == text) ? "" : text;
- }
- static public String formatText(String text, int maxLength)
- {
- return (null == text) ? "" : text.length() <= maxLength ? text : text.substring(0,maxLength);
- }
- static public String formatValuta(int value)
- {
- DecimalFormatSymbols symbols = new DecimalFormatSymbols();
- symbols.setGroupingSeparator('.');
- symbols.setDecimalSeparator(',');
- DecimalFormat nf = new DecimalFormat("#,##0.00", symbols);
- return nf.format(value);
- }
- static public String formatValuta(double value)
- {
- DecimalFormatSymbols symbols = new DecimalFormatSymbols();
- symbols.setGroupingSeparator('.');
- symbols.setDecimalSeparator(',');
- DecimalFormat nf = new DecimalFormat("#,##0.00", symbols);
- return nf.format(value);
- }
- static public String formatValutaNoSeparator(double value)
- {
- DecimalFormatSymbols symbols = new DecimalFormatSymbols();
- symbols.setDecimalSeparator(',');
- symbols.setGroupingSeparator('.');
- DecimalFormat nf = new DecimalFormat("#,##0.00", symbols);
- return nf.format(value).replaceAll("\\.","");
- }
- static public String formatValutaNoSeparator(float value)
- {
- DecimalFormatSymbols symbols = new DecimalFormatSymbols();
- symbols.setDecimalSeparator(',');
- symbols.setGroupingSeparator('.');
- DecimalFormat nf = new DecimalFormat("#,##0.00", symbols);
- return nf.format(value).replaceAll("\\.","");
- }
- static public String formatValutaNoSeparator(int value)
- {
- DecimalFormat nf = new DecimalFormat("###");
- return nf.format(value).replaceAll("\\.","");
- }
- static public String formatDecimalValue(int value)
- {
- DecimalFormatSymbols symbols = new DecimalFormatSymbols();
- symbols.setGroupingSeparator('.');
- symbols.setDecimalSeparator(',');
- DecimalFormat nf = new DecimalFormat("#,##0", symbols);
- return nf.format(value);
- }
- static public String formatValue(int value)
- {
- DecimalFormat nf = new DecimalFormat("###");
- return nf.format(value);
- }
- static public String formatValue(double value)
- {
- DecimalFormatSymbols symbols = new DecimalFormatSymbols();
- symbols.setDecimalSeparator(',');
- DecimalFormat nf = new DecimalFormat("##0.00");
- return nf.format(value);
- }
- static public String formatDate(Date date)
- {
- return new SimpleDateFormat("dd/MM/yyyy").format(date);
- }
- static public String formatDate(Date date, String pattern)
- {
- if(pattern == null)
- {
- pattern = "dd/MM/yyyy";
- }
- return new SimpleDateFormat(pattern).format(date);
- }
- static public String formatDateTime(Date date)
- {
- return new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(date);
- }
- static public String formatTime(Date date)
- {
- return new SimpleDateFormat("HH:mm:ss").format(date);
- }
- static public String formatTime(Date date, String separator)
- {
- String pattern = "HH:mm:ss";
- if(separator != null)
- {
- pattern = pattern.replaceAll(":", separator);
- }
- return new SimpleDateFormat(pattern).format(date);
- }
- static public String formatBoolean(boolean b)
- {
- return Boolean.toString(b);
- }
- //
- // static public String formatDate(String date) {
- // try {
- // return formatDate(new SimpleDateFormat("dd/MM/yyyy").parse(date));
- // } catch (ParseException e) {
- // try {
- // return formatDate(new SimpleDateFormat().parse(date));
- // } catch (ParseException e1) {
- // return formatText(date);
- // }
- // }
- // }
- public static String formatNumber2Dec(String sValue)
- {
- String model = "###,###,###,###,##0.00";
- return formatNumberModel(sValue, model);
- }
- public static String formatNumberModel(String sValue, String model)
- {
- String formatVal = "";
- if (sValue == null || sValue.equals(""))
- return "";
- DecimalFormatSymbols mySymbol = new DecimalFormatSymbols();
- mySymbol.setDecimalSeparator(',');
- mySymbol.setGroupingSeparator('.');
- DecimalFormat myFormatter = new DecimalFormat(model, mySymbol);
- myFormatter.setGroupingSize(3);
- formatVal = myFormatter.format(new Double(sValue));
- return formatVal;
- }
- public static String unformatNumber(String sValue)
- {
- try
- {
- String formatVal = "";
- if (sValue == null || sValue.equals(""))
- return "";
- //sValue = StringUtility.ReplaceSubString(sValue,".","");
- //sValue = StringUtility.ReplaceSubString(sValue,",",".");
- sValue = sValue.replaceAll("\\.", "").replaceAll(",", ".");
- DecimalFormatSymbols mySymbol = new DecimalFormatSymbols();
- mySymbol.setDecimalSeparator('.');
- String model = "################.################";
- DecimalFormat myFormatter = new DecimalFormat(model, mySymbol);
- myFormatter.setGroupingSize(16);
- formatVal = myFormatter.format(new Double(sValue));
- return formatVal;
- }
- catch (Exception e)
- {
- e.printStackTrace();
- return "";
- }
- }
- public static String formatPercentuale(double value) throws TransformException
- {
- DecimalFormatSymbols dfs = new DecimalFormatSymbols();
- dfs.setDecimalSeparator(',');
- NumberFormat format = new DecimalFormat("###.0###",dfs);
- //imposto il numero di decimali per la formattazione
- if(NullValue.NUMBER == 3)
- {
- format.setMaximumFractionDigits(3);
- format.setMinimumFractionDigits(3);
- }
- else
- {
- format.setMaximumFractionDigits(3);
- format.setMinimumFractionDigits(3);
- }
- format.setMaximumIntegerDigits(3);
- format.setMinimumIntegerDigits(1);
- format.setGroupingUsed(false);
- String fValue = format.format(value);
- // Se il numero di decimali ? automatico...
- if(NullValue.NUMBER == 3)
- {
- // ...elimina gli zero meno significativi
- while(fValue.endsWith("0"))
- fValue = fValue.substring(0,fValue.length()-1);
- if(fValue.endsWith(","))
- fValue = fValue.substring(0,fValue.length()-1);
- }
- return fValue;
- }
- public static String formatAliquota(double value) throws TransformException
- {
- DecimalFormatSymbols dfs = new DecimalFormatSymbols();
- dfs.setDecimalSeparator(',');
- NumberFormat format = new DecimalFormat("###.0####",dfs);
- //imposto il numero di decimali per la formattazione
- format.setMaximumFractionDigits(4);
- format.setMinimumFractionDigits(4);
- format.setMaximumIntegerDigits(3);
- format.setMinimumIntegerDigits(1);
- format.setGroupingUsed(false);
- String fValue = format.format(value);
- // Se il numero di decimali ? automatico...
- if(NullValue.NUMBER == 4)
- {
- // ...elimina gli zero meno significativi
- while(fValue.endsWith("0"))
- fValue = fValue.substring(0,fValue.length()-1);
- if(fValue.endsWith(","))
- fValue = fValue.substring(0,fValue.length()-1);
- }
- return fValue;
- }
- public static String formatSuperDouble(double value) throws TransformException
- {
- DecimalFormatSymbols dfs = new DecimalFormatSymbols();
- dfs.setDecimalSeparator(',');
- NumberFormat format = new DecimalFormat("###.0##########",dfs);
- //imposto il numero di decimali per la formattazione
- format.setMaximumFractionDigits(10);
- format.setMinimumFractionDigits(10);
- format.setMaximumIntegerDigits(1);
- format.setMinimumIntegerDigits(1);
- format.setGroupingUsed(false);
- String fValue = format.format(value);
- // // Se il numero di decimali ? automatico...
- // if(NullValue.NUMBER == 4)
- // {
- // // ...elimina gli zero meno significativi
- // while(fValue.endsWith("0"))
- // fValue = fValue.substring(0,fValue.length()-1);
- // if(fValue.endsWith(","))
- // fValue = fValue.substring(0,fValue.length()-1);
- // }
- return fValue;
- }
- /** Versione 'semplice' di una sprintf
- * @param fmt la stringa di formattazione
- * @param params i parametri
- * @return la stringa formattata
- */
- final static public String simpleSprintf(final String fmt, final Object ... params)
- {
- return String.format(fmt.replaceAll("\\{[0-9]*\\}", "\\%s"), params);
- }
-
- /**
- * Restituisce un array di stringe divise secondo la lunghezza <len> massima
- * @param str
- * @param len
- * @return
- */
- public static List<String> formatTextStrutturaPDF(String str, int len)
- {
- if(StringUtils.isEmpty(str) || len == 0)
- return new ArrayList<String>();
- final String SEPARATOR = ", ";
- String[] aStr = str.split(SEPARATOR);
- List<String> result = new ArrayList<String>();
- for(int index = 0, size = aStr.length; index < size; index++)
- {
- if(result.size() == 0)
- result.add(aStr[index] + SEPARATOR);
- else if(!StringUtils.isEmpty(aStr[index]))
- {
- String tmp = result.get(result.size()-1);
- if((tmp.length() + aStr[index].length()) <= len)
- {
- result.remove(result.size()-1);
- tmp = tmp.substring(0, tmp.length()-2);
- result.add(tmp + SEPARATOR + aStr[index] + SEPARATOR);
- }
- else
- {
- result.add(aStr[index] + SEPARATOR);
- }
- }
- }
- if(result.size() > 0){
- String tmp = result.get(result.size()-1);
- tmp = tmp.substring(0, tmp.length()-2);
- result.remove(result.size()-1);
- result.add(tmp);
- }
- return result;
- }
-
- /**
- * Restituisce la Stringa passata come parametro troncata dopo #len caratteri
- * @param str
- * @param len
- * @return
- */
- public static String trunkString(String str, int len)
- {
- if(StringUtils.isEmpty(str))
- return NullValue.STRING;
- str = str.trim();
- if(str.length() <= len)
- return formatText(str);
- return formatText(str.substring(0, len))+"...";
- }
-
- public static String formatNumeroTessera(int numeroTessera) {
- return String.format("%07d", numeroTessera);
- }
- }
|